`

Flex全局错误处理Global Error Handler在AIR 2.0和Flash Player 10.1中使用

阅读更多
http://flex4jiaocheng.com/blog/347

The new global error handler enables developers to write a single handler to process all runtime errors that weren’t part of a try/catch statement. Improve application reliability and user experience by catching and handling unexpected runtime errors and present custom error messages.
  新的全局错误处理(Global Error Handler)让开发人员编只写一个处理过程就能在运行时处理所有的错误,而不需要一个一个得try/catch。它能捕获和处理未知的运行时错误,并自定义错误消息,大大改善应用程序的可靠性和用户体验。

Registering for uncaught errors is hugely convenient, but it doesn’t entirely excuse developers from handling errors where they’re most likely to happen. For instance, you should still always register for things like IOError events and other errors that are likely to happen at some point, and that you can usually recover from. But starting with AIR 2.0 (and FP 10.1), you will also be able to register globally for errors that you weren’t able to anticipate, and handle them at least somewhat gracefully.
  虽然为不能捕捉的错误注册监听是非常的方便,但这不能成为开发者逃避处理最可能发生错误地方的借口。例如,你必须为像IOError这一类的事件注册监听,因为他们随时可能发生,并且你可以修复他们。但是从Air2.0(FP 10.1)开始,你将能为那些你不能预先估计的错误进行全局注册监听,而且从某种程度来说优雅的解决他们。

The big question is what you do once you’ve caught an unhandled error. That will probably depend on the application. Since it’s likely that a function exited without executing all the way through, there’s no telling what state your app will be in. The safest thing to do is probably to log the error, show a very contrite dialog box, and then exit the app (after all, if the error had been predictable and easy to recover from, you should have caught it explicitly). You might try sending the error message to your server, including instructions for emailing the log file to a support email address, or if it’s an internal application, provide a telephone extension to call for someone to come take a look. It’s entirely up to you. We provide the API, you provide the solution.
  最大的问题是你该要做什么当捕捉到一个未经处理的错误。这可能取决于应用程序,因为你可以随时的结束一个程序,所以你将不会被告知你的程序的状态,最安全的方式可能就是用一个日志记录器来记录错误,然后再结束程序。(如果这个错误是可被预测的而且很容易被解决,你应该明确的捕捉它。)你应该尝试让应用程序发送错误消息到你的服务器,包括将其寄送到服务邮箱地址,如果这是一个内部程序,你也应该提供一个电话能让某人来解决问题。最后提醒一句,我们提供API,你提供解决方案。


API找到的都是WindowedApplication的例子,那我就来写一个Application的吧。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx"
   applicationComplete="onApplicationComplete()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;

private function onApplicationComplete():void{
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
}

private function onUncaughtError(event:UncaughtErrorEvent):void{
var message:String = event.error.getStackTrace();
if(!message){
if(event.error is Error){
message = Error(event.error).message;
}
else if(event.error is ErrorEvent){
message = ErrorEvent(event.error).text;
}
else{
message = event.error.toString();
}
}
Alert.show(message);
}

private function onCauseError(e:MouseEvent):void{
var foo:String = null;
Alert.show(foo.length.toString());
}
]]>
</fx:Script>
<s:Button label="Cause TypeError" click="onCauseError(event)" verticalCenter="0" horizontalCenter="0"/>
</s:Application>

虽然全局错误处理(Global Error Handler)能catch到未知Error,但是catch到的信息是和FP的版本有关的。
release版Flash Player



debug版Flash Player





Adobe文章:http://blogs.adobe.com/cantrell/archives/2009/10/global_error_handling_in_air_20.html

P.S.
想要使用全局错误处理(Global Error Handler)必须将你的SDK或Flash Builder升级至最新版本。
Flex4 SDK:http://www.adobe.com/cfusion/entitlement/index.cfm?e=flex4sdk
Flash Builder 4.0.1 updater:http://www.adobe.com/support/flex/downloads_updaters.html
当然你的AIR和FP也要升级至标题中写的版本。
  • 大小: 3.3 KB
  • 大小: 7.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics