Today I stumbled upon what feels like strange behaviour. In short, if there's an Error thrown in an event listener, it won't be caught in the catch block of the code that dispatched the event. Instead, it will be dispatched by the uncaughtErrorEvents property of LoaderInfo. (See the code below.)
Does this imply that event dispatching is asynchronous? Despite that the stack traces always include the event dispatching, and thus seem to imply it's synchronous? Or just the fact that it happens at a lower level (within the C++ code of the flash player, perhaps?) impacts try-catch blocks? In any case, I think it would really help if we updated the documentation under LoaderInfo.uncaughtErrorEvents [1] and the description of the UncaughtErrorEvents class [2] to include this. [1] http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/LoaderInfo.html#uncaughtErrorEvents [2] http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/UncaughtErrorEvents.html CODE: <?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*" windowComplete="windowedapplication1_creationCompleteHandler(event)" width="800" height="600"> <fx:Script> <![CDATA[ import mx.events.AIREvent; protected function windowedapplication1_creationCompleteHandler(event:AIREvent):void { loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleUncaughtClientError); addEventListener("throwErrorPlease", onTestDispatched); } protected function onTestDispatched(event:Event):void { throw(new Error("juust testin'")); } protected function handleUncaughtClientError(event:UncaughtErrorEvent):void { trace("uncaught:" + event.error); } protected function button1_clickHandler(event:MouseEvent):void { try {dispatchEvent(new Event("throwErrorPlease"));} catch(e:Error) { trace("caught:" + e); } } ]]> </fx:Script> <s:Button label="throw" click="button1_clickHandler(event)"/> </s:WindowedApplication>
