411/090
Flash-Global Error Handling in AIR 2.0 and Flash 10.1
Global Error Handling全局变量处理,以下内容翻译自http://blogs.adobe.com/cantrell/archives/2009/10/global_error_handling_in_air_20.html,人肉翻译哈哈。
在MAX presentation中最受欢迎的一个内容是global error handling (GEH),GEH可以让你处理全部捕获的errors(包括同步error和异步error事件)。下边是GEH的一个工作示例:
< ?xml version="1.0" encoding="utf-8"?>
<mx :WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="onApplicationComplete();">
</mx><mx :Script>
< ![CDATA[
private function onApplicationComplete():void
{
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
}
private function onUncaughtError(e:UncaughtErrorEvent):void
{
// Do something with your error.
trace(e.error, e.errorID);
}
private function onCauseError(e:MouseEvent):void
{
var foo:String = null;
try
{
trace(foo.length);
}
catch (e:TypeError)
{
trace("This error is caught.");
}
// Since this error isn't caught, it will cause the global error handler to fire.
trace(foo.length);
}
]]>
</mx>
<mx :Button label="Cause TypeError" click="onCauseError(event);"/>
注册捕获errors是非常便利的,不过开发人员也不需要捕获所有的errors。举个例子,你还是需要在某些地方捕获IOError事件,以方便修复。不过在AIR 2.0 (和 FP 10.1)里,你可以注册GEH来更方便地捕获到这些error。
现在的一个大问题是,当你捕获到一个没有定位的error时你该怎么做?这要看是什么样的Application。它可能就像一个函数一路执行到底,也没有告诉你现在app的状态。最安全的做法大概是log error,显示一个醒目的对话框,然后退出app(当然,如果error可以被预测,并且易于恢复,你应该明确地捕获它)。
你可以尝试将error信息发送到你的服务器,包括通过电子邮件发送error log到技术支持的电子邮件地址。如果这是个单机版的软件,或许提供电话或者地址让用户来反馈也不错。
It's entirely up to you. We provide the API, you provide the solution.