Flex全局错误处理Global Error Handler代码兼容运行于低版本Flash Player

Global Error Handler是从Flash Player 10.1.x开始才加入的功能,如果将其代码运行于Flash Player 10.0.x或更低版本的FP时,swf将会出错。
那么用什么方法才能使Global Error Handler的代在Flash Player 10.1.x时能正常使用,而在低版本FP中又不使swf出错呢?

请看如下代码:

private function setGlobalErrorHandler(event:FlexEvent):void{
  if( !loaderInfo.hasOwnProperty("uncaughtErrorEvents") ) return;
  Object(loaderInfo).uncaughtErrorEvents.addEventListener("uncaughtError", onUncaughtError);
  function onUncaughtError(event:Event):void{
    var message:String = Object(event).error.getStackTrace();
    if(!message){
      if(Object(event).error is Error){
        message = Error(Object(event).error).message;
      }
      else if(Object(event).error is ErrorEvent){
        message = ErrorEvent(Object(event).error).text;
      }
     else{
        message = Object(event).error.toString();
      }
    }
  }
}

P.S.
不能把.addEventListener("uncaughtError", onUncaughtError);改成.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
因为低版本FP中没有UncaughtErrorEvent,会使swf出错。

原文地址:https://www.cnblogs.com/bmate/p/1868213.html