At some points in my application, I've a try-catch block such as: This happens inside classes that are not in the display list (not Sprites, nor any kind of DisplayObject), but extend EventDispatcher. Those classes reside in externally loaded SWF (in case that matters).
try {
... some logic that may throw Error
} catch (e:Error) {
var errorEvent:ErrorEvent = new ErrorEvent(ErrorEvent.ERROR, true);
errorEvent.text = e.getStackTrace();
dispatchEvent(errorEvent);
}
In the root class, this is what I have:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtErrorHandler);
loaderInfo.uncaughtErrorEvents.addEventListener(ErrorEvent.ERROR, onErrorEventHandler);
stage.addEventListener(ErrorEvent.ERROR, onErrorEventHandler);
protected function onUncaughtErrorHandler(event:UncaughtErrorEvent):void
{
var message:String;
if (event.error is Error) {
message = event.error.getStackTrace();
} else if (event.error is ErrorEvent) {
message = ErrorEvent(event.error).text;
} else {
message = event.error.toString();
}
trace(message);
}
protected function onErrorEventHandler(event:ErrorEvent):void
{
trace(event.text);
}
Neither handlers are called, but those error events bubble up and I see them in console, and as popups in debug mode, but how do I listen to them in a root class?
I do this because I don't want the error to interrupt execution of the main thread or the particular business logic.