How can I enable unwind semantics for C++-style exceptions?

I am having an issue with my project compiling. I am integrating the SqlApi++ library to allow my project to post gameplay metrics to a MySQL server, but my project fails to compile when I build it for Development mode. While it works when built for Development-Editor, it throws a C4530 warning when building for Development in version 4.0, 4.1, and 4.1.1 of the engine.

The compiler complains about me using a try/catch statement to wrap my calls to the server. Specifically, it errors out on the start of the try/catch block itself, such that using

try {} catch (SAException&) {}

will cause the compilation to fail. The error returned is:

warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc

The odd thing is, though, I know another team is using the exact same library, but have never received this error nor had to manually specify the /EHsc flag when compiling their project (at least to my knowledge). How can I set this flag when building my project?

Any help on this matter would be greatly appreciated. Thanks!

–Alex

We normally don’t use exceptions, but you can enable them by setting bForceEnableExceptions to true in BuildConfigratuion.xml (locate that file in Solution)

Unfortunately, that option doesn’t seem to be supported in 4.1.1. I added it to the BuildConfiguration, and the BuilderTool broke.

Well, you can always hack VCToolChain.cs in UnrealBuildTool to add /EHsc (look up the code where it gets added) :slight_smile: This is the dirtiest solution possible though (which will work though), I suggest that you first try to sort out the problem why you could not do that through BuildConfiguration.

Bleh. Didn’t mean to delete that.

Anyway, it doesn’t seem like bForceEnableExceptions is a member in the BuildConfiguration.cs file, and editing the BuildConfiguration.xml file to

<Configuration>

<BuildConfiguration>

<bForceEnableExceptions>True</bForceEnableExceptions>

<BuildConfiguration>

<Configuration>

causes the UBT to break with the following error:

Unhandled Exception: UnrealBuildTool.BuildException: ERROR: BuildConfiguration Loading: field ‘bForceEnableExceptions’ doesn’t exist or is either non-public or non-static.

Ah, sorry for misleading, it might have been a recent addition. I suggest to look inside VCToolChain.cs to see what other build configuration options influence adding /EHsc switch and setting them to true for your project/target.

No worries. :slight_smile:

I’ll take a look at the tool chain and see if that will fix the issue.

Ah, it looks like bForceEnableExceptions is a member in the UEBuildConfiguration.cs file

Hi

did that work for you? I tried that on a Mac using Xcode and it was ignoed using Unreal 4.4.
It seems that if you build for Mac and iOS that its hardcoded to have --no-exceptions as compiler flag. I tried to modify this in UnrealEngine/Engine/Source/Programs/UnrealBuildTool/IOS/IOSToolChain.cs
and compiled UnrealEditor using XCode. This solved the compiler errors so far but the Xcode project file generation did not work that well and created a project with 26 targets which comiles insanly slow.

It would be great if the build tool chain would respect the mentioned config. params so there wouldn’t be a reason to compile unreal editor on your own.

Has anybody experiences with this issue?

thanks
ciao Matthias

is there an alternative way to handle exceptions in third party libraries? Why is this not enabled by default?

You can compile the third party library with exceptions enabled, assuming that it catches them itself. If its API mandates that you need to catch exceptions thrown by the library, then it’s a bad design since exceptions crossing dynamic library boundaries is an unreliable feature that will only work if the library in question is using the same C++ runtime.

Adding this:

UEBuildConfiguration.bForceEnableExceptions = true;

Inside the {ProjectName}.Build.cs constructor seems to solve the problem.

Thanks to this open source project for the hint :slight_smile:

that did the trick for me, thanks :wink:

Thanks, that works for me too. Epic should not make it difficult for us to put at least a crash handler in C++ functions. Debugging of shipping versions is hard enough. A crash handler is a minimum requirement

I had added some try/catch statements to my code after which I could no longer package a build. Adding this statement to the build.cs in the constructor worked for me. Thank you.

For 4.16 and newer, you need:

bEnableExceptions = true;
2 Likes

For Unreal 4.18 in the [ProjectName].Build.cs file, the constructor parameter is now ReadOnlyTarget rules and I got an error using: bForceEnableExceptions = true;
I made it work by editing [ProjectName].Target.cs in the directory above. Adding bForceEnableExceptions = true; there fixed my build so I could use exceptions.

Just tested this, it works! And it also works for plugins.

But this broke again with 4.19.
Anyone having a solution?

Yes it is broken in 4.19.