当我在调试 Flash 播放器中运行 Flex 应用程序时,一旦发生意外情况,就会弹出异常。然而,当客户使用该应用程序时,他不会使用调试闪存播放器。在这种情况下,他不会弹出异常,但他的 UI 无法正常工作。
因此,出于可支持性的原因,我希望捕获 Flex UI 中任何地方可能发生的任何异常,并在 Flex 内部弹出窗口中显示错误消息。通过使用 Java,我只需将整个 UI 代码封装在 try/catch block 中,但对于 Flex 中的 MXML 应用程序,我不知道在哪里可以执行这样的常规 try/catch。
最佳答案
在 Flex 3 中,无法收到有关未捕获异常的通知。Adobe 已意识到该问题,但我不知道他们是否计划创建解决方法。
目前唯一的解决方案是将 try/catch 放在逻辑位置,并确保您正在监听调度它们的任何事件的 ERROR(或 Web 服务的 FAULT)事件。
编辑:此外,实际上不可能捕获事件处理程序引发的错误。我已经记录了bug关于 Adobe Bug 系统。
更新 2010-01-12: Flash 10.1 现在支持全局错误处理和 AIR 2.0 (均处于测试版),并通过订阅 UNCAUGHT_ERROR 来实现事件LoaderInfo.uncaughtErrorEvents 。以下代码摘自code sample on 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
}
}
关于apache-flex - 如何捕获Flex中的所有异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/101532/