简体   繁体   中英

event listener for flex/air action script errors

I'm trying to add an event listener to my air application that would prevent the "ActionScript error" window from appearing, so I can handle the error within my application.

I was able to find a little information about this from adobe . I'm just not sure what I should be listening for.

It depends quite a lot on what error is thrown, and why.

Your best bet is to carefully read the ActionScript documentation and add listeners to react to all of the errors that have explicit ErrorEvents (such as IOErrorEvent and SecurityErrorEvent ). Those are usually related to network and/or file access, and security issues.

For most other errors, there is the try {} catch() {} finally {} statements. This tutorial might be a good place to start.

And if all else fails, there's UncaughtErrorEvent .

But you should really be using that one as a last resort, not as a magic bullet - the best error handling is a) trying to prevent errors from being thrown in the first place (make sure all variables are properly initialized, test for null, etc.), and b) handling expected runtime errors by catching them explicitly, in order to keep the application running and stable.

You have a couple options. As you know, exception handling is not always possible for certain asynchronous operations.

First off, you need to know what object is responsible for the asynchronous operation that is causing the error. The most sensible approach would be to add the necessary error event handlers to this object.

For instance, a URLLoader performs asynchronous operations; and it's failure can only be handled by adding error event listeners. For example:

var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

Another 'catch-all' option is to take advanage of the new UncaughtErrorEvent feature of Flash Player 10.1. For this to work, you need to attach the uncaught error handler to the loader of the main application; this will catch everything! For example:

loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, loaderErrorHandler);

private function loaderErrorHandler(e:UncaughtErrorEvent):void {
  if(event.error is Error) {
     // handle error from embedded SWF
  }
  // suppress error dialog
  e.preventDefault();
}

The last option may not be the best approach as it promotes the swallowing of exceptions instead of addressing and handling the problem properly; nevertheless it can be useful in certain unique circumstances (embedding SWFs).

The window won't appear if you're running the standard version of Flash Player.

It will manifest only as a dialog box on the debugger versions of the browser plug-ins and stand-alone players, as a message in the output panel in the authoring player, and as an entry in the log file for Adobe Flex Builder 3. It will not manifest at all in the release versions of Flash Player or AIR.

Source : here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM