如何捕捉Flex中的所有exception?

当我在debuggingFlash播放器中运行一个Flex应用程序时,一旦出现意想不到的事情,就会立即popup一个exception。 但是,当客户使用该应用程序时,他不使用debugging闪存播放器。 在这种情况下,他没有得到exceptionpopup,但他的用户界面不工作。

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

在Flex 3中没有办法通知未捕获的exception.Adobe知道这个问题,但我不知道他们是否计划创build一个解决方法。

唯一的解决scheme就是把try / catch放在合理的地方,并且确保你正在监听错误(或者错误的web服务)事件。

编辑:此外,捕获从事件处理程序抛出的错误实际上是不可能的。 我已经logging了Adobe Bug System上的一个错误 。

2010年1月12日更新: Flash 10.1和AIR 2.0现在都支持全局error handling(均处于testing阶段),并通过订阅LoaderInfo.uncaughtErrorEvents的UNCAUGHT_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 } } 

在Adobe bugpipe理系统中有一个bug /function请求。 投票给它,如果这对你很重要。

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

它在Flex 3.3中工作。

  if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){ IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler); } 

请注意,错误FP-444(以上)链接到http://labs.adobe.com/technologies/flashplayer10/features.html#developer ,自2009年10月以来,这将是可能的10.1,目前,10月28日, 2009年还没有发布 – 所以我想我们会看到,如果这是真的,当它被释放

替代接受的答案,使用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

我正在使用flex 4.我尝试loaderInfo.UncaughtErrorEvents,但loaderInfo未初始化,所以它给了我空引用错误。 然后我尝试了root.loaderInfo.UncaughtErrorEvents和相同的故事。 我尝试了sprite.root.UncaughtErrorEvents ,但没有任何精灵对象,我创build了一个,但它没有工作。 最后我试了一下

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

猜猜看是什么,就像魔术一样。 检查这个

它适用于Flex 3.5和Flash Player 10:

 <?xml version="1.0" encoding="utf-8"?> 

  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> 

谢谢

我将事件监听器附加到“root”,它对我有用:

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

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

 event.preventDefault(); 

所以:

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

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

现在你可以使用loader信息:

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