Flecs.NET icon indicating copy to clipboard operation
Flecs.NET copied to clipboard

Better exception handling for systems and observers.

Open BeanCheeseBurrito opened this issue 10 months ago • 0 comments

In .NET 9, exceptions thrown inside systems cause the app to terminate with no stack trace. Exceptions can also no longer be caught with a debugger. This can be fixed by setting the DOTNET_LegacyExceptionHandling environment variable to use the old exception handler. Setting an environment variable is not always convenient, so we need to figure out a way to get stacktraces logged.

var system = world.System()
    .Each(static (Iter it, int i) =>
    {
        throw new Exception();
    });

system.Run();

DOTNET_LegacyExceptionHandling=0 (New exception handling)

terminate called after throwing an instance of 'PAL_SEHException'

DOTNET_LegacyExceptionHandling=1 (Old exception handling)

Unhandled exception. System.Exception: Exception of type 'System.Exception' was thrown.
   at Playground.<>c.<Main>b__2_0(Iter it, Int32 i) in /_/Flecs.NET/src/Flecs.NET.Examples/Playground.cs:line 48
   at Flecs.NET.Core.Invokers.EachIterCallbackDelegate.Invoke[TFieldGetter](Fields& fields, Int32 count, InvokerCallback callback) in /_/Flecs.NET/src/Flecs.NET/Generated/Invoker/Each/T0.g.cs:line 66
   at Flecs.NET.Core.Invokers.EachIterCallbackDelegate.Flecs.NET.Core.Invokers.IEachInvoker.Invoke[TFieldGetter](Fields& fieldData, Int32 count, InvokerCallback callback)
   at Flecs.NET.Core.Invoker`16.Each[TInvoker](Iter it, InvokerCallback callback) in /_/Flecs.NET/src/Flecs.NET/Core/Invoker.cs:line 113
   at Flecs.NET.Core.BindingContext.Functions.EachIterCallbackDelegate(ecs_iter_t* iter) in /_/Flecs.NET/src/Flecs.NET/Generated/BindingContext/Each/T0.g.cs:line 26
   at Flecs.NET.Core.BindingContext.Functions.IteratorCallback(ecs_iter_t* iter) in /_/Flecs.NET/src/Flecs.NET/Core/BindingContext/Functions.cs:line 74

In the long term, I'd like to implement a managed version of World.Progress() that'll move system execution to C# instead of letting flecs handle it.

BeanCheeseBurrito avatar Apr 18 '25 10:04 BeanCheeseBurrito