• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Try/catching errors occuring in synchronous event handlers

New Here ,
Apr 14, 2015 Apr 14, 2015

Copy link to clipboard

Copied

Hi,

I know that using try/catch in flash player it is possible to catch only synchronous errors, but recently I ran into code in our application which I strongly believe is synchronous but it behaves as if it wasn't.

In example below I create event listener and inside try/catch I dispatch event. Handler function throws error. Executing code stops when error is thrown, so message "after throwing error" won't be logged, but try/catch block catches nothing and code executes as if nothing happened after event dispatch.

Dispatching event on element executes handler method immediately so it is synchronous execution. Event call stack which is displayed in debug versions of flash player, shows every function from creation of class to execution of handler.

Generally flash applications relays heavily on events and in my case it caused ours users projects to be irreversibly corrupted, because of this continuing to work after error, which I even can't properly detect and handle, cause it's going throught 5 or more event handlers and adding try/catch to each handler would create enormous chaos in my code. So my question is why does flash player behaves like this? Are there any ways to tell compiled SWF file to treat such cases as synchronous calls, f.e. compilation parameters?

package

{

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.events.EventDispatcher;

    import flash.text.TextField;

    public class TestApp extends Sprite

    {

        public var cTextField:TextField;

        public function TestApp()

        {

            cTextField = new TextField();

            cTextField.width = 300;

            cTextField.height = 200;

            addChild(cTextField);

            onAppCreated();

        }

        protected function onAppCreated():void

        {

            var eventName:String = "my_event";

            var caughtError:Boolean = false;

            var dispatcher:EventDispatcher = new EventDispatcher();

            dispatcher.addEventListener(eventName, handler);

            try

            {

                dispatcher.dispatchEvent(new Event(eventName));

            } catch (error:Error)

            {

                caughtError = true;

            }

            cTextField.text += caughtError ? "caught error\n" : "error wasn't caught\n";

        }

        protected function handler(event:Event):void

        {

            cTextField.text += "before throwing error\n";

            throw new Error("throw error");

            cTextField.text += "after throwing error\n";

        }

    }

}

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Apr 14, 2015 Apr 14, 2015

Copy link to clipboard

Copied

I moved this into the Beta forums because this audience is more technical.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
May 01, 2015 May 01, 2015

Copy link to clipboard

Copied

Okay, nobody bit.  Sorry. 

The only way to really figure out what is going on is to look at a running example with a C++ debugger.  I'm particularly curious about whether this problem happens in a specific browser/os combination (we may be working around a quirk of that platform), or if it's something that happens everywhere.

The most effective way to proceed is going to be to file a bug with a simplified, executable example (source and compiled SWF, ideally) that demonstrates the problem.  This will help me route the bug to a developer and get you an answer in the shortest time possible. 

You can file a bug here:

http://bugbase.adobe.com/

If you reply here with the bug number, I'll get it assigned to an engineer.

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 11, 2015 May 11, 2015

Copy link to clipboard

Copied

Sorry for my late response. I already created issue in bugbase at the beginning of April, but so far I got no response.
Here is link to bug I created:
Bug#3963313 - Handling errors in flash player


I added there compiled version of my test app.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 08, 2015 May 08, 2015

Copy link to clipboard

Copied

In regards to your try/catch statement, it is functioning properly. The point of a try/catch is to try a block and catch any errors that specific code creates. That code inside the "try" is simply running a dispatchEvent() method call and that code is executing just fine. The event handler responding to that event dispatch is where the "throw error" is occurring. If you were to "throw new Error()" inside the "try", it would probably work as you are expecting it to.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 11, 2015 May 11, 2015

Copy link to clipboard

Copied

wadedwalker‌ Try/catch works properly with nested functions. If core part of my example looked as below, try/catch would have caught exception:

protected function onAppCreated():void

{

  var caughtError:Boolean = false;

  try

  {

  handler();

  } catch (error:Error)

  {

  caughtError = true;

  }

  cTextField.text += caughtError ? "caught error\n" : "error wasn't caught\n";

}

protected function handler():void

{

  cTextField.text += "before throwing error\n";

  throw new Error("throw error");

  cTextField.text += "after throwing error\n";

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 11, 2015 May 11, 2015

Copy link to clipboard

Copied

I understand the example you provided works. That is because you are directly calling a method that throws an error. In your original example, calling dispatchEvent() is not the same as calling handler() directly. You could say you are calling handler() indirectly, but the handler() method is being called by the event system so that is why the try/catch is not receiving the error. To my knowledge, this is the expected behavior and not a bug.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2015 May 21, 2015

Copy link to clipboard

Copied

I think what you are actually looking for is an event handler for uncaught errors: UncaughtErrorEvent - Adobe ActionScript® 3 (AS3 ) API Reference

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 21, 2015 May 21, 2015

Copy link to clipboard

Copied

LATEST

That's not it. I already have UncaughtErrorHandler in my application and it's used to log errors which happen in client applications, but I can't take actions complex based on them because they can show everywhere in application and it's hard to be sure what to do with concrete error from global context.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines