提问人:Yaba 提问时间:9/19/2008 更新时间:7/23/2020 访问量:31799
如何在 Flex 中捕获所有异常?
How to catch all exceptions in Flex?
问:
当我在调试 Flash 播放器中运行 Flex 应用程序时,一旦发生意外情况,就会弹出异常。但是,当客户使用应用程序时,他不会使用调试 Flash 播放器。在这种情况下,他不会弹出异常,但他的UI不起作用。
因此,出于可支持性的原因,我想捕获 Flex UI 中任何可能发生的异常,并在 Flex 内部弹出窗口中显示错误消息。通过使用 Java,我只会将整个 UI 代码封装在 try/catch 块中,但是对于 Flex 中的 MXML 应用程序,我不知道在哪里可以执行这种通用的 try/catch。
答:
在 Flex 3 中,无法收到未捕获的异常的通知。Adobe已经意识到了这个问题,但我不知道他们是否打算创建一个解决方法。
目前唯一的解决方案是将 try/catch 放在逻辑位置,并确保您正在侦听 ERROR(或 Web 服务的 FAULT)事件,以查找调度它们的任何内容。
编辑:此外,实际上不可能捕获从事件处理程序引发的错误。我在 Adobe Bug 系统上记录了一个错误。
更新 2010-01-12:Flash 10.1 和 AIR 2.0(均为 beta 版)现在支持全局错误处理,这是通过订阅 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
}
}
评论
((IEventDispatcher)loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", handlerFunction)
if (loaderInfo.hasProperty("uncaughtErrorEvents") { }
Adobe Bug 管理系统中有一个 bug/功能请求。如果它对你很重要,请投票支持它。
http://bugs.adobe.com/jira/browse/FP-444
请注意,错误 FP-444(上图)链接到 http://labs.adobe.com/technologies/flashplayer10/features.html#developer,自 2009 年 10 月以来显示这将在 10.1 中成为可能,目前,2009 年 10 月 28 日仍未发布 - 所以我想我们会在它发布时看看这是否属实
它适用于 Flex 3.3。
if(loaderInfo.hasOwnProperty("uncaughtErrorEvents")){
IEventDispatcher(loaderInfo["uncaughtErrorEvents"]).addEventListener("uncaughtError", uncaughtErrorHandler);
}
接受答案的替代方法,使用 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
我将事件侦听器附加到“根”,这对我有用:
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 项目。
我正在使用 flex 4。
我尝试过,但loaderInfo没有初始化,所以它给了我空引用错误。然后我尝试了同样的故事。
我试过了,但没有精灵对象,我创建了一个,但它不起作用。最后我试了loaderInfo.UncaughtErrorEvents,
root.loaderInfo.UncaughtErrorEvents
sprite.root.UncaughtErrorEvents
systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,globalUnCaughtErrorHandler.hanleUnCaughtError);
你猜怎么着,它就像魔术一样工作。 检查这个
它适用于 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>
谢谢
现在,您可以使用加载程序信息:
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.
}
评论