Haxe uncaught global exception handler

509 Views Asked by At

I am writing UI of an application using Haxe for CPP targets. I need to intercept haxe error/exceptions before it crashes the App.
Below is an example of code which crashes the application:

@:final private function callFoo(classA : IInterface) : Void
{
    if ((mClassLevelVariable != null) && (classA != mClassLevelVariable))
    {
        throw new Error("Can not work with " + Type.getClassName(Type.getClass((classA))));
    }
}  

I need to intercept the crash before error like given above crashes the application. Do we have any support in Haxe like Java provides Thread.UncaughtExceptionHandler?

1

There are 1 best solutions below

2
On BEST ANSWER

You could simply wrap your main() in a try-catch:

class Main {
    static function main() {
        try {
            entryPoint();
        } catch (e:Any) {
            // do something with e
        }
    }
}

That's pretty much how for instance OpenFL implements Flash's uncaught error event as well.

Note that not all exceptions can be caught this way in hxcpp. Null pointer exceptions for instance can only be caught if HXCPP_CHECK_POINTER is defined.