Try/catch not preventing crashing

Hi !
When I type faulty code that the compiler doesn’t see, I press play and Unreal Editor crashes and i have to relaunch everything again.

I tried inserting the code in a try/catch but Unreal Editor still crashes.
Here is the code that will cause Unreal to crash :

  FVector PlayerViewPointLocation;
  FRotator PlayerViewPointRotation;
  /// OUT means this variables will be modified by the function.
  GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
                                                             OUT PlayerViewPointLocation,
                                                             OUT PlayerViewPointRotation
                                                             );

  try {
  
  UE_LOG(LogTemp, Warning, TEXT("Location: %s"), PlayerViewPointLocation); //instead of *PlayerViewPointLocation.ToString() 
  }catch(int e) {
UE_LOG(LogTemp, Error, TEXT("there is an uncatched error right there, c++ !!"));
}`

What I had forgotten was, first the method .ToString(), and that is should be a pointer and not the variable ( *PlayerViewPointLocation.ToString() )

I have two questions :

  1. Can i prevent crashes when I write code I am not sure about ? Should the try catch not prevent crashing ?
  2. Where are the logs, and where to find the relevant reports ?

Thanks !

That definitely shouldn’t be occurring; are you sure it’s solely things you type that cause the crash? You’re right in saying that the compiler shouldn’t be seeing it until you build/compile.

Thanks for replying.

I added the faulty code in my post to make more sense and be more concise.

Forgetting the * for the pointer as well as forgetting to call .ToString(), caused the compiler not to see the error ( both mistakes together otherwise the compiler sees an error); Then the code caused Unreal to crash on runtime

Ah, good thing we’re on the same page now, I thought you meant that it was looking at code you hadn’t compiled yet :smiley:

I think there’s really no way to deal with errors when it comes to the UE4 macros, unfortunately that’s the one part of UE4 C++ that you just have to get right. The way it’s set up doesn’t directly correspond with the compiler like you pointed out, but I’m not sure there’s really a way to combat it (since I don’t think UE4 throws actual C++ errors when its macros fail, but rather just crash).

1 Like

Where is the final bracket ?

Maybe its a hot reload issue, have you tried packaging your code into a game executable and running it from there ?

Yes try catch should prevent crashes like this. Using macros should not be a problem because macros exist only during compile time and not run time.

I am by no means an expert with C++ so if your code runs fine as a standalone and crashes only inside the editor then its most likely a hot reload issues which AFAIK is problematic by nature anyway.

1 Like

Exceptions are disabled by default and discourged by Epic for use in Unreal and to use assertions instead. Also UE_LOG(Fatal, ...) will stop execution if you want that.

1 Like

Privacy & Terms