How to catch all exceptions in Flex?

When I launch a Flex application in a debug flash player, I get an exception as soon as something unexpected happens. However, when the client uses the application, it does not use the debug flash player. In this case, it does not receive a popup exception, but the user interface does not work.

So, for support reasons, I would like to get any exception that might happen anywhere in the Flex interface and present an error message in the Flex internal popup. Using Java, I would just encapsulate all the user interface code in a try / catch block, but with MXML applications in Flex I don't know where I could execute such a common try / catch.

+43
flex exception error-handling
Sep 19 '08 at 12:43
source share
9 answers

Unable to receive notification of uncaught exceptions in Flex 3. Adobe is aware of the problem, but I do not know if they plan to create a workaround.

The only solution that stands in it is to put try / catch in logical places and make sure that you are listening to the ERROR event (or FAULT for web services) for everything that sends them.

Edit: Furthermore, it is actually not possible to catch the error received from the event handler. I registered a bug on the Adobe Bug system.

Update 2010-01-12: Global error handling is now supported in Flash 10.1 and AIR 2.0 (both in beta), and is achieved by signing UNCAUGHT_ERROR LoaderInfo.uncaughtErrorEvents . The following code is taken from the liveocs sample code :

public class UncaughtErrorEventExample extends Sprite { public function UncaughtErrorEventExample() { loaderInfo.uncaughtErrorEvents.addEventListener( UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler); } private function uncaughtErrorHandler(event:UncaughtErrorEvent):void { if (event.error is Error) { var error:Error = event.error as Error; // do something with the error } else if (event.error is ErrorEvent) { var errorEvent:ErrorEvent = event.error as ErrorEvent; // do something with the error } else { // a non-Error, non-ErrorEvent type was thrown and uncaught } } 
+50
Sep 19 '08 at 13:49
source share
β€” -

Adobe's error management system has an error / feature. Vote for it if it is important to you.

http://bugs.adobe.com/jira/browse/FP-444

+9
Nov 04 '08 at 15:28
source share

It works in Flex 3.3.

  if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){ IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler); } 
+4
Mar 23 '10 at 13:48
source share

Please note that the FP-444 error (above) refers to http://labs.adobe.com/technologies/flashplayer10/features.html#developer , which from October 2009 shows that this will be possible with 10.1, which is currently the time, October 28, 2009, has not yet been released - so I think we will see if this is true when it is released.

+3
Oct 28 '09 at 12:09
source share

Alternative to accepted answer using try-catch. I think it’s slower, but easier to read.

 try { loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", onUncaughtError); } catch (e:ReferenceError) { var spl:Array = Capabilities.version.split(" "); var verSpl:Array = spl[1].split(","); if (int(verSpl[0]) >= 10 && int(verSpl[1]) >= 1) { // This version is 10.1 or greater - we should have been able to listen for uncaught errors... d.warn("Unable to listen for uncaught error events, despite flash version: " + Capabilities.version); } } 

Of course, you will need to use the updated 10.1 playerglobal.swc to compile this code: http://labs.adobe.com/downloads/flashplayer10.html

+3
Jun 11 2018-10-06T00:
source share

I am using flex 4. I tried loaderInfo.UncaughtErrorEvents, but loaderInfo was not initialized, so it gave me a null link error. Then I tried root.loaderInfo.UncaughtErrorEvents and the same story. I tried sprite.root.UncaughtErrorEvents , but there was no sprite object, I created it, but it did not work. Finally i tried

systemManager.loaderInfo.uncaughtErrorEvents.addEventListener (UncaughtErrorEvent.UNCAUGHT_ERROR, globalUnCaughtErrorHandler.hanleUnCaughtError);

And guess that it works like magic. this

+3
May 05 '11 at 14:11
source share

It works in Flex 3.5 and flash player 10:

 <?xml version="1.0" encoding="utf-8"?> 

  protected function application1_addedToStageHandler(event:Event):void{ if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){ IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler); } sdk.text = "Flex " + mx_internal::VERSION; } private function uncaughtErrorHandler(e:*):void{ e.preventDefault(); var s:String; if (e.error is Error) { var error:Error = e.error as Error; s = "Uncaught Error: " + error.errorID + ", " + error.name + ", " + error.message; } else { var errorEvent:ErrorEvent = e.error as ErrorEvent; s = "Uncaught ErrorEvent: " + errorEvent.text; } msg.text = s; } private function unCaught():void { var foo:String = null; trace(foo.length); } ]]> </mx:Script> <mx:VBox> <mx:Label id="sdk" fontSize="18"/> <mx:Button y="50" label="UnCaught Error" click="unCaught();" /> <mx:TextArea id="msg" width="180" height="70"/> </mx:VBox> 

thank

+3
Jul 06 2018-11-11T00:
source share

I connected the event listener to "root", which worked for me:

 sprite.root.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError); 

In the debugging Flash Player, this will still be an error, but in the non-debugging version the error will appear in the Flash Player dialog box and the handler will respond. To stop the dialog box from appearing, add:

 event.preventDefault(); 

So:

  private function onUncaughtError(event:UncaughtErrorEvent):void { event.preventDefault(); // do something with this error } 

I used this in AIR, but I assume that it works for standard AS3 projects.

+2
Feb 14 '11 at 17:06
source share

Now you can, using the bootloader information:

http://www.adobe.com/devnet/flex/articles/global-exception-handling.html

Checkout: loaderInfo.uncaughtErrorEvents.addEventListener (UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);

 private function onUncaughtError(e:UncaughtErrorEvent):void { // Do something with your error. } 
0
Mar 04 '15 at 19:21
source share



All Articles