6 Replies Latest reply on Jun 28, 2011 2:25 PM by mattrichard

    How to catch UncaughtErrors from a loaded SWF?

    mattrichard Level 1

      From the documentation (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/UncaughtEr rorEvent.html), uncaught errors in a child swf should bubble up to the loaderInfo of the top-level swf.  I can't seem to get that behavior to work.  Here's the code for a small sample Flex app:

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/mx">
           
           <fx:Script>
                <![CDATA[
                                         
      
                     private function uncaughtErrorHandler(event:Event):void {
                          event.preventDefault();
                          trace("caught error");
                     }
           
                     override protected function createChildren():void {
                          systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
                          super.createChildren();
                     }
      
                ]]>
           </fx:Script>
           
           <s:SWFLoader source="B.swf" />
           
      </s:Application>
      

       

       

      B.swf is a blank page compiled in Flash CS5 (targetting AS3 and Flash 10) that throws an ErrorEvent in frame 1 (throw new ErrorEvent(ErrorEvent.ERROR, true, true);).  When I run the app, the error caused in B is not caught.  What am I doing wrong here?

       

      Thanks,

       

      Matt

        • 1. Re: How to catch UncaughtErrors from a loaded SWF?
          Flex harUI Adobe Employee

          Throw an Error from the main app to be sure your handler works, then throw

          an Error from B (not an ErrorEvent) and see what happens.

          1 person found this helpful
          • 2. Re: How to catch UncaughtErrors from a loaded SWF?
            mattrichard Level 1

            Per your sugesstion, I tried throwing an error (throw new Error("A");), and it was not caught either.  Am I registering for the uncaught errors in the wrong way?  I pretty much copied and pasted straight from the documentation.

            • 3. Re: How to catch UncaughtErrors from a loaded SWF?
              Flex harUI Adobe Employee

              Look ok to me.  I assume you threw the error after you installed the

              handler.  Make sure you are on FP 10.1 or later.

              • 4. Re: How to catch UncaughtErrors from a loaded SWF?
                mattrichard Level 1

                According to Capabilities.version, my FP is WIN 10,2,152,26

                 

                Edit: I just updated to 10,3,181,34 and my problem persists.

                 

                Edit2: I have realized that I was stopping my debug run too soon (as soon as the IDE reported the error, I was stopping it).  If I allow the app to continue running after the error occurrs, the in-app error is caught successfully, but the error in B.swf is still not caught.

                 

                Here's the changed source just to rule out the possibility that I did something dumb:

                 

                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                  xmlns:mx="library://ns.adobe.com/flex/mx">
                     
                     <fx:Script>
                          <![CDATA[
                               
                               private function uncaughtErrorHandler(event:Event):void {
                                    event.preventDefault();
                                    trace("caught error");
                               }
                     
                               override protected function createChildren():void {
                                    systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
                                    trace(Capabilities.version);
                                                                     
                                    super.createChildren();
                
                                    throw new Error("A");
                               }
                
                          ]]>
                     </fx:Script>
                     
                     <s:SWFLoader source="B.swf" />
                     
                </s:Application>
                
                • 5. Re: How to catch UncaughtErrors from a loaded SWF?
                  Peter Blazejewicz Level 4

                  Hello,

                   

                  using that page as guide:

                  http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/events/Uncau ghtErrorEvent.html#includeExamplesSummary

                  (last sample on where to insert handlers) what about:

                   

                   

                  <s:SWFLoader id="swfLoader" init="swfLoaderInitHandler(event)" source="B.swf"/>

                   

                   

                  and:

                   

                   

                  protected function swfLoaderInitHandler(event:Event):void

                  {

                       if(!swfLoader.content) return;

                       swfLoader.content.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAU GHT_ERROR,

                            uncaughtErrorHandler);

                  };

                  protected function uncaughtErrorHandler(errorEvent:UncaughtErrorEvent):void

                  {

                       if(errorEvent.error is Error)

                       {

                            var e:Error = errorEvent.error as Error;

                            // my error is 1009 - it is expected here

                            const NULL_ERROR:uint = 1009;

                            if(e.errorID == NULL_ERROR)

                            {

                                 errorEvent.preventDefault();

                                 Alert.show(e.errorID+" "+e.message);

                            };

                            // else fall through

                       };

                       // else fall through

                  }

                   

                   

                  B is supposed to fail:

                   

                   

                  private var emptyObject:Object = null;

                  public function B()

                  {

                       flash.utils.setTimeout(throwErrorNow, 100);

                  };

                  protected function throwErrorNow():void

                  {

                       emptyObject.numericValue();

                  }

                   

                   

                   

                  regards,

                  Peter

                  • 6. Re: How to catch UncaughtErrors from a loaded SWF?
                    mattrichard Level 1

                    Peter, thank you for your post!  I don't understand why the uncaught error is not bubbling up to the parent app's loaderInfo, but registering directly to the loaded SWF's loaderInfo (the loader also worked) fixed our problem.

                     

                    Thanks,

                     

                    Matt