Does addEventListener(ErrorEvent.ERROR, handler)
handle all type of error event, for example, IOErrorEvent.IO_ERROR
, SecurityErrorEvent.SECURITY_ERROR
, and other all error events?
addEventListener()
version of try catch(e:Error)
(e:Error can catch all type of errors).You can add error handlers to the UncaughtErrorEvents
object:
loaderInfo.uncaughtErrorEvents.addEventListener(
UncaughtErrorEvent.UNCAUGHT_ERROR, errorHandler);
function errorHandler(e:UncaughtErrorEvent):void {
if(event.error is Error) {
// handle error
}
// suppress error dialog
e.preventDefault();
}
This is only possible in Flash Player 10.1 and above.
You can find more information here: flash.events.UncaughtErrorEvents
This can be especially helpful for handling exceptions from a loaded SWF. I assume you have a good reason for doing this?
Each event type is registered as a different String
, so the only way to catch all events of varying types is to listen for uncaught errors that get relayed by a special UncaughtErrorEvents
dispatcher. Notably, this exists on any DisplayObject
's loaderInfo
property @ DisplayObject.loaderInfo.uncaughtErrorEvents
.
Demonstrating 3 ways to receive uncaught errors...
private var loader:Loader = new Loader();
public function MyDocumentClass ()
{
// 1: Listen for all errors in the application:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// 2: Listen for errors from the child swf being loaded:
loader.load(new URLRequest("file.swf"));
loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// 3: Listen for errors from Loader doing the loading:
loader.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// This seems like it would work, but wasn't working in tests I ran:
stage.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
}
}
From Adobe's documentation...
An
UncaughtErrorEvent
(extendsErrorEvent
) object is dispatched by an instance of theUncaughtErrorEvents
class when an uncaught error occurs. An uncaught error happens when an error is thrown outside of anytry..catch
blocks or when anErrorEvent
object is dispatched with no registered listeners. The uncaught error event functionality is often described as a "global error handler."
The UncaughtErrorEvents
object can be accessed in two ways...
LoaderInfo.uncaughtErrorEvents
-- to detect uncaught errors in code defined in the same SWF.An object that dispatches an uncaughtError event when an unhandled error occurs in the SWF that's loaded by this Loader object. An uncaught error happens when an error is thrown outside of any try..catch blocks or when an ErrorEvent object is dispatched with no registered listeners.
Note that a Loader object's uncaughtErrorEvents property dispatches events that bubble through it, not events that it dispatches directly. It never dispatches an uncaughtErrorEvent in the target phase. It only dispatches the event in the capture and bubbling phases. To detect an uncaught error in the current SWF (the SWF in which the Loader object is defined) use the LoaderInfo.uncaughtErrorEvents property instead.
Loader.uncaughtErrorEvents
-- to detect uncaught errors in code defined in the SWF loaded by a Loader object.An object that dispatches an uncaughtError event when an unhandled error occurs in code in this LoaderInfo object's SWF file. An uncaught error happens when an error is thrown outside of any try..catch blocks or when an ErrorEvent object is dispatched with no registered listeners.
This property is created when the SWF associated with this LoaderInfo has finished loading. Until then the uncaughtErrorEvents property is null. In an ActionScript-only project, you can access this property during or after the execution of the constructor function of the main class of the SWF file. For a Flex project, the uncaughtErrorEvents property is available after the applicationComplete event is dispatched.
Some important details from Adobe's documentation...
When an uncaughtError event happens, even if the event is handled, execution does not continue in the call stack that caused the error. If the error is a synchronous error, any code remaining in the function where the error happened is not executed. Consequently, it is likely that when an uncaught error event happens, your application is in an unstable state. Since there can be many causes for an uncaught error, it is impossible to predict what functionality is available. For example, your application may be able to execute network operations or file operations. However, those operations aren't necessarily available.
When content is running in a debugger version of the runtime, such as the debugger version of Flash Player or the AIR Debug Launcher (ADL), an uncaught error dialog appears when an uncaught error happens. For those runtime versions, the error dialog appears even when a listener is registered for the uncaughtError event. To prevent the dialog from appearing in that situation, call the UncaughtErrorEvent object's preventDefault() method.
if you want to catch all the errors in your application you should definitely use try-catch blocks. By using addEventListener you are adding listener to a specific object and you will catch the errors only there.
精彩评论