如何在 Flex 中捕获所有异常?

How to catch all exceptions in Flex?

提问人:Yaba 提问时间:9/19/2008 更新时间:7/23/2020 访问量:31799

问:

当我在调试 Flash 播放器中运行 Flex 应用程序时,一旦发生意外情况,就会弹出异常。但是,当客户使用应用程序时,他不会使用调试 Flash 播放器。在这种情况下,他不会弹出异常,但他的UI不起作用。

因此,出于可支持性的原因,我想捕获 Flex UI 中任何可能发生的异常,并在 Flex 内部弹出窗口中显示错误消息。通过使用 Java,我只会将整个 UI 代码封装在 try/catch 块中,但是对于 Flex 中的 MXML 应用程序,我不知道在哪里可以执行这种通用的 try/catch。

apache-flex 异常 错误处理

评论


答:

52赞 Richard Szalay 9/19/2008 #1

在 Flex 3 中,无法收到未捕获的异常的通知。Adobe已经意识到了这个问题,但我不知道他们是否打算创建一个解决方法。

目前唯一的解决方案是将 try/catch 放在逻辑位置,并确保您正在侦听 ERROR(或 Web 服务的 FAULT)事件,以查找调度它们的任何内容。

编辑:此外,实际上不可能捕获从事件处理程序引发的错误。我在 Adobe Bug 系统上记录了一个错误

更新 2010-01-12:Flash 10.1AIR 2.0(均为 beta 版)现在支持全局错误处理,这是通过订阅 LoaderInfo.uncaughtErrorEventsUNCAUGHT_ERROR 事件来实现的。以下代码摘自 livedocs 上的代码示例

public class UncaughtErrorEventExample extends Sprite
{
    public function UncaughtErrorEventExample()
    {
        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
        }
    }

评论

0赞 Assaf Lavie 3/7/2010
Flash 10.1 中的全局错误处理是否需要使用 flex 3.5?4?或者它也可以在 Flex 3 中使用吗?
2赞 Richard Szalay 3/8/2010
我上面的代码需要 Flex 4。但是,如果您使用 ,它应该适用于针对 10.1 运行的任何 SDK,因为这些属性将在运行时存在于播放器中。您甚至可以将其包装起来,以确保它不会在 Flash 9/10 中中断(当然,错误处理不起作用,但不会崩溃)((IEventDispatcher)loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", handlerFunction)if (loaderInfo.hasProperty("uncaughtErrorEvents") { }
1赞 Wouter Coekaerts 10/20/2010
@Richard的评论:这确实符合你期望它的工作方式,但不幸的是,它没有。如果以 Flash Player 9 为目标进行编译,并在 Flash Player 10.1 上运行它,loaderInfo[“uncaughtErrorEvents”] 仍然不可用!我的解释是:Flash 播放器在运行时会查看您的 swf 所针对的玩家,并“隐藏”该版本中尚未包含的功能。
0赞 Richard Szalay 10/21/2010
@Wouter - 我也看到了这种行为。随意对我的错误进行投票/添加评论:bugs.adobe.com/jira/browse/FB-27199
3赞 Christophe Herreman 10/27/2010
补充一下:如果您在 Flash Player 的调试版本中运行,则仍会弹出常规运行时错误对话框。为了防止这种情况,请在全局错误处理程序中调用 event.preventDefault()。
9赞 Samuel Crank 11/4/2008 #2

Adobe Bug 管理系统中有一个 bug/功能请求。如果它对你很重要,请投票支持它。

http://bugs.adobe.com/jira/browse/FP-444

3赞 Peter V. Mørch 10/28/2009 #3

请注意,错误 FP-444(上图)链接到 http://labs.adobe.com/technologies/flashplayer10/features.html#developer,自 2009 年 10 月以来显示这将在 10.1 中成为可能,目前,2009 年 10 月 28 日仍未发布 - 所以我想我们会在它发布时看看这是否属实

4赞 user299956 3/23/2010 #4

它适用于 Flex 3.3。

 if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
    IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
 }
3赞 aaaidan 6/11/2010 #5

接受答案的替代方法,使用 try-catch。我认为速度较慢,但阅读起来更简单。

try {
    loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", onUncaughtError);
} catch (e:ReferenceError) {
    var spl:Array = Capabilities.version.split(" ");
    var verSpl:Array = spl[1].split(",");

    if (int(verSpl[0]) >= 10 &&
        int(verSpl[1]) >= 1) {
        // This version is 10.1 or greater - we should have been able to listen for uncaught errors...
        d.warn("Unable to listen for uncaught error events, despite flash version: " + Capabilities.version);
    }
}

当然,您需要使用最新的 10.1 playerglobal.swc 才能成功编译此代码: http://labs.adobe.com/downloads/flashplayer10.html

2赞 neave 2/15/2011 #6

我将事件侦听器附加到“根”,这对我有用:

sprite.root.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);

在调试 Flash Player 中,这仍然会出错,但在非调试版本中,该错误将出现在 Flash Player 的对话框中 - 然后处理程序将做出响应。若要停止显示对话框,请添加:

event.preventDefault();

所以:

    private function onUncaughtError(event:UncaughtErrorEvent):void
    {
        event.preventDefault();
        // do something with this error
    }

我在 AIR 中使用它,但我认为它也适用于标准 AS3 项目。

3赞 Rose 5/5/2011 #7

我正在使用 flex 4。 我尝试过,但loaderInfo没有初始化,所以它给了我空引用错误。然后我尝试了同样的故事。 我试过了,但没有精灵对象,我创建了一个,但它不起作用。最后我试了loaderInfo.UncaughtErrorEvents,root.loaderInfo.UncaughtErrorEventssprite.root.UncaughtErrorEvents

systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,globalUnCaughtErrorHandler.hanleUnCaughtError);

你猜怎么着,它就像魔术一样工作。 检查这个

3赞 Jefferson 7/6/2011 #8

它适用于 Flex 3.5 和 Flash 播放器 10:

  <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" addedToStage="application1_addedToStageHandler(event)">
    <mx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            
            protected function application1_addedToStageHandler(event:Event):void{              
                if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
                    IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
                }
                
                sdk.text = "Flex " + mx_internal::VERSION;
            }
            
            private function uncaughtErrorHandler(e:*):void{
                e.preventDefault();
                
                var s:String;

                if (e.error is Error)
                {
                    var error:Error = e.error as Error;
                    s = "Uncaught Error: " + error.errorID + ", " + error.name + ", " + error.message;
                }
                else
                {
                    var errorEvent:ErrorEvent = e.error as ErrorEvent;
                    s = "Uncaught ErrorEvent: " + errorEvent.text;
                }
                
                msg.text = s;
            }
            
            private function unCaught():void
            {
                var foo:String = null;
                trace(foo.length);
            }
        ]]>
    </mx:Script>
    <mx:VBox>
        <mx:Label id="sdk" fontSize="18"/>
        <mx:Button y="50" label="UnCaught Error" click="unCaught();" />
        <mx:TextArea id="msg" width="180" height="70"/>
    </mx:VBox>
</mx:Application>

谢谢

0赞 Pablo 3/5/2015 #9

现在,您可以使用加载程序信息:

http://www.adobe.com/devnet/flex/articles/global-exception-handling.html

收款处:

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

private function onUncaughtError(e:UncaughtErrorEvent):void
{
    // Do something with your error.
}