addEventListener(ErrorEvent.ERROR, handler) 是否处理actionscript3中所有类型的错误事件?

发布于 2024-12-08 19:36:52 字数 281 浏览 1 评论 0原文

addEventListener(ErrorEvent.ERROR, handler) 是否处理所有类型的错误事件,例如 IOErrorEvent.IO_ERRORSecurityErrorEvent.SECURITY_ERROR 等所有错误事件?

我正在寻找 addEventListener() 版本的 try catch(e:Error)(e:Error 可以捕获所有类型的错误)。

Does addEventListener(ErrorEvent.ERROR, handler) handle all type of error event, for example, IOErrorEvent.IO_ERROR, SecurityErrorEvent.SECURITY_ERROR, and other all error events?

I'm looking for addEventListener() version of try catch(e:Error)(e:Error can catch all type of errors).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-12-15 19:36:52

您可以将错误处理程序添加到 UncaughtErrorEvents 对象:

loaderInfo.uncaughtErrorEvents.addEventListener(
    UncaughtErrorEvent.UNCAUGHT_ERROR, errorHandler);

function errorHandler(e:UncaughtErrorEvent):void {
   if(event.error is Error) {
      // handle error
   }
   // suppress error dialog
   e.preventDefault();
}

这只能在 Flash Player 10.1 及更高版本中实现。

您可以在此处找到更多信息:flash.events。 UncaughtErrorEvents

这对于处理加载的 SWF 中的异常特别有用。我想你这样做有一个充分的理由吗?

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?

不回头走下去 2024-12-15 19:36:52

每个事件类型都注册为不同的String,因此捕获所有不同类型的事件的唯一方法是监听未捕获错误由特殊的 UncaughtErrorEvents 调度程序转发。值得注意的是,它存在于任何 DisplayObjectloaderInfo 属性 @ DisplayObject.loaderInfo.uncaughtErrorEvents 上。

演示接收未捕获错误的 3 种方法...

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
    }
}

来自 Adobe 文档...

UncaughtErrorEvent(扩展 ErrorEvent)对象由
未捕获错误时 UncaughtErrorEvents 类的实例
发生。当错误抛出到外部时,就会发生未捕获的错误
任何 try..catch 块或当使用以下命令调度 ErrorEvent 对象时
没有注册的听众。未捕获的错误事件功能是
通常被描述为“全局错误处理程序”。

可以通过两种方式访问​​ UncaughtErrorEvents 对象...

  • LoaderInfo.uncaughtErrorEvents -- 用于检测代码中未捕获的错误在同一个 SWF 中定义。

    <块引用>

    当未处理的错误时调度 uncaughtError 事件的对象
    此 Loader 对象加载的 SWF 中发生错误。一个
    当错误抛出到任何外部时,就会发生未捕获的错误
    try..catch 块或者当没有调用 ErrorEvent 对象时
    注册听众。

    请注意,Loader 对象的 uncaughtErrorEvents 属性会调度
    通过它冒泡的事件,而不是它直接分派的事件。
    它永远不会在目标阶段调度 uncaughtErrorEvent。它仅
    在捕获和冒泡阶段调度事件。检测一个
    当前 SWF 中未捕获的错误(Loader 对象所在的 SWF)
    已定义)请改用 LoaderInfo.uncaughtErrorEvents 属性。

  • Loader.uncaughtErrorEvents -- 检测由 Loader 对象加载的 SWF 中定义的代码中未捕获的错误。

    <块引用>

    当未处理的错误时调度 uncaughtError 事件的对象
    此 LoaderInfo 对象的 SWF 文件中的代码发生错误。一个未捕获的
    当错误抛出到任何 try..catch 块之外时,就会发生错误
    或者当没有注册的情况下调度 ErrorEvent 对象时
    听众。

    当 SWF 与此 LoaderInfo 关联时,将创建此属性
    已完成加载。在此之前,uncaughtErrorEvents 属性为
    无效的。在仅使用 ActionScript 的项目中,您可以访问此属性
    在 main 的构造函数执行期间或之后
    SWF 文件的类。对于 Flex 项目,uncaughtErrorEvents
    属性在 applicationComplete 事件发生后可用
    已发送。

Adobe 文档中的一些重要详细信息...

当发生 uncaughtError 事件时,即使事件被处理,
执行不会在导致错误的调用堆栈中继续。
如果错误是同步错误,则保留在
发生错误的函数不会被执行。因此,它是
当发生未捕获的错误事件时,您的应用程序可能会
处于不稳定状态。由于导致未捕获的原因有很多
错误,无法预测哪些功能可用。
例如,您的应用程序可能能够执行网络
操作或文件操作。然而,这些操作并不
必然可用。

当内容在运行时的调试器版本中运行时,例如
Flash Player 的调试器版本或 AIR 调试启动器 (ADL),
当发生未捕获的错误时,会出现未捕获的错误对话框。为了
这些运行时版本,即使侦听器也出现错误对话框
已注册 uncaughtError 事件。为了防止对话框
出现这种情况,调用 UncaughtErrorEvent 对象的
PreventDefault() 方法。

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 (extends ErrorEvent) object is dispatched by an
instance of the UncaughtErrorEvents class when an uncaught error
occurs. 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. 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.

逆蝶 2024-12-15 19:36:52

如果你想捕获应用程序中的所有错误,你绝对应该使用 try-catch 块。通过使用 addEventListener,您可以将侦听器添加到特定对象,并且您只能捕获那里的错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文