Flex全局错误处理/Global Error Handling in AIR 2.0 and Flash 10.1

Global Error Handling in AIR 2.0 and Flash 10.1
http://blogs.adobe.com/cantrell/archives/2009/10/global_error_handling_in_air_20.html

非常酷的特性,AS3也有全局错误处理了!

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="onApplicationComplete();">

<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.
  if (e.error is Error)
  {
    var error:Error = e.error as Error;
    trace(error.errorID, error.name, error.message);
  }
  else
  {
    var errorEvent:ErrorEvent = e.error as ErrorEvent;
    trace(errorEvent.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:Script>

<mx:Button label="Cause TypeError" click="onCauseError(event);"/>

</mx:WindowedApplication>
posted @ 2010-11-23 11:37  RIA爱好者  阅读(990)  评论(0编辑  收藏  举报