# C++ best error handling practice?

**URL:** <https://forums.unrealengine.com/t/c-best-error-handling-practice/438687>\
**Category:** C++\
**Tags:** nullptr, unreal-engine, question, CPP\
**Created:** [December 2, 2018, 12:51am UTC](https://forums.unrealengine.com/t/c-best-error-handling-practice/438687 "2018-12-02T00:51:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alessa](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alessa/32/770370_2.png) [@Alessa](https://forums.unrealengine.com/u/Alessa)\
**Post date:** [December 2, 2018, 12:51am UTC](https://forums.unrealengine.com/t/c-best-error-handling-practice/438687/1 "2018-12-02T00:51:04Z")

</div>

So I’ve been learning C++ the last few months, and given that I have certain values that are specified by designers exposed on a given class, I need to validate and ensure that these values aren’t nullptrs.

Right now, the way I’m handling it is a large if statement - I’m trying to effectively log to the user which of these classes are unset. Is there a better way to handle/do this?

I’d really appreciate any help here!

 ![261081-2018-12-02-01-48-25-window.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/2/4/224b5952ff60090f3b813f170cd09992c6c7876b.png)

---

<div class="post-metadata">

**Author:** ![ScottMichaud](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ScottMichaud](https://forums.unrealengine.com/u/ScottMichaud)\
**Post date:** [December 2, 2018, 1:05am UTC](https://forums.unrealengine.com/t/c-best-error-handling-practice/438687/2 "2018-12-02T01:05:13Z")

</div>

Make a CHECK\_NULL() with one parameter that accepts a reference. If that reference is null, output to log and return. Its usage will look like:

```
CHECK_NULL(LilyCharacter, LilySceneTransition, "Invalid Lily Player Character on Game Mode");
CHECK_NULL(PlayerCameraManager, LilySceneTransition, "Invalid Player Camera Manager");
... etc.

```

You can add parameters and have it in a well-used header file… or you could simply define one per cpp file that has class-specific data baked in. That depends on how you want to architect your code.

Its declaration will look something like:

```
#define CHECK_NULL(myPointer, myLogClass, myMessage) if(myPointer) UE_LOG(myLogClass, Error, TEXT(myMessage), *this->GetFName()->toString()); return

```

Make sure that’s all one line!

Sorry I didn’t test it (I came across your tweet but didn’t have my PC setup correctly) but yeah. Hope this helps.

---

<div class="post-metadata">

**Author:** ![Alessa](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alessa/32/770370_2.png) [@Alessa](https://forums.unrealengine.com/u/Alessa)\
**Post date:** [December 2, 2018, 1:30am UTC](https://forums.unrealengine.com/t/c-best-error-handling-practice/438687/3 "2018-12-02T01:30:36Z")

</div>

This works absolutely perfectly, just a minor typo on ToString() in the macro definition. And now I know how to use Macros!

For prosperity, another suggestion on this twitter was to use checkf or ensure - more documentation is found here: [Asserts | Unreal Engine Documentation](https://docs.unrealengine.com/en-us/Programming/Assertions)

But now I know how to define macros, and honestly for this project I love now having simple definitions for checking validity. Thanks, Scott! 🙂
