Try/Catch fails to catch exception?

UE4 crashes randomly about 1% of the time at one specific line in my code with Unhandled Exception: EXCEPTION_ACCESS_VIOLATION. I have no idea what’s causing the crash so I put a Try/Catch at that line figuring at least the game wouldn’t crash if it happens and just fail gracefully instead. But the game is still crashing anyway even with the Try/Catch.

How is that possible? What can I do to stop the crash? How could I possibly debug this to try to figure what’s wrong? How can the error say “Unhandled Exception” when there’s a Try/Catch? Doesn’t the Try/Catch, by definition, means it’s handled? Isn’t that the whole point of why God created the Try/Catch in the first place?

My code at MyCharacter.cpp where the crash happens at line 278:

The stack trace:

[Link to Code and full Stack Trace in text format.][3]

[Link to project.][4]

1 Like

I have the same problem.How did you solve it?

UE4 code don’t support exceptions (it simply does not use it). Instead function just return false, nullptr or some other state if it fails.

UE4 also practice code assertion, if UE4 detect unexpected condition (it does check) it crash the engine with failed condition in log. It reason why you barely see Access Violation from the engine fault… it will asset fail before that happens.

1 Like

I was never able to track down exactly what was causing that specific problem. But it was probably the garbage collector deleting something that it shouldn’t. Basically you have to put a UPROPERTY() on every class variable that you can, so the GC works properly with them. Once I put UPROPERTY() on everything, those errors stopped happening.

2 Likes

Just to expand on the point @vib mentioned, you don’t need to put on everything, just any type of pointers that you want to hold on to. For non-UObject type pointers, use the std c++ smart pointers. Basic datatypes and structures are not GC’d.

1 Like

try catch is a language feature

Unfortunately (or fortunately, depending on how you see it :sweat_smile: ) in UE you’re not using Vanilla C++. I believe the way to handle error is with the check, verify, ensure families of functions. See : Asserts in Unreal Engine | Unreal Engine 5.2 Documentation

And actually its preferable from a performance standpoint as well, which is why using try/catch is unsupported in UE territory.

1 Like