Why not use a Try-Catch Block instead of hardcoding to check the null Condition?

in Lecture 109 we used if statements to check whether the PressurePlate is assigned or Null,

Why Can’t we use a Try-Catch Block Around the code to handle the exception instead of checking the condition?

is using if condition a better choice

try
	{
		if (PressurePlate->IsOverlappingActor(ActorThatOpens))
		{
			OpenDoor(DeltaTime);
		}
	}
	catch (const std::exception&)
	{
		UE_LOG(LogTemp, Error, TEXT("ActorThatOpens is Null"));
	}`Preformatted text`

i don't know how to exactly implement try-catch in UE4 but used to use this in android apps.
1 Like
  1. There’s nothing wrong with ActorThatOpens being nulllptr here, as it’s never dereferenced. PressurePlate is the problematic one if it’s nullptr as it’s being dereferenced.
  2. Exceptions are disabled in Unreal due to their poor performance.
  3. There’s no such thing as a null pointer exception in C++ so that try-catch wouldn’t do anything.
  4. Exceptions are for exceptional scenarios and should not be used for control flow. The unhappy path is slow due to stack unwinding. How to improve C++ exceptions/error handling in general, is one of the big things being discussed right now (see here for a talk on one of the proposals)
  5. An assertion would be more fitting than an exception here.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms