landelare / ue5coro

A C++20 coroutine implementation for Unreal Engine 5 that feels almost native.
BSD 3-Clause Clear License
543 stars 48 forks source link

Why are exceptions not supported? #3

Closed broly closed 1 year ago

broly commented 2 years ago

Whats wrong with exceptions and coroutines together in UE? Is it even possible?

landelare commented 1 year ago

Unreal Engine disables exceptions (and RTTI) by default, the engine API overall is not designed with exceptions in mind, therefore I don't support them either.

If you want to use exceptions anyway you can enable them for your module, it's only unhandled exceptions that reach unhandled_exception() and the check(). You don't need this plugin's explicit support for handling exceptions within a coroutine, that's part of the language.

What's your use case? I put together some super basic unhandled exception rethrowing on the exception_handling_test branch if you'd like to try.

broly commented 1 year ago

Hi Laura! Thank you for answer. I've enabled exceptions for my separate module where I using coroutines for my game backend. I using custom RPC with coroutines in my case. And sometimes I want to add some timeout and remote exception handling (if the RPC returns the exceptions, not normal result). It looks like this:

FMyTask Test()
{
    try
    {
       auto[ X, Y ] = co_await Service.RemoteMethod(params...);  // RemteMethod returns the awaitable
    }
    catch (FRemoteException& Exc)
    {
         UE_LOG(LogTemp, Warning, TEXT("Can't proceed this function"));
    }
}

And this works! But little bit later it entails the undefined behavior (engine can very often (not always) stall forever or throw the access violation exception at random point of UE). Why it can be happened? Could you look at my implementation here: https://pastebin.com/DgWhcBee https://pastebin.com/tSdfavQg ? I thought the exception disabled in your plugin by same reason, but now I'm not sure.

landelare commented 1 year ago

I hope you'll understand that I don't provide support for other, unrelated coroutine implementations in this bug tracker.