flex全局异常

52 篇文章 0 订阅
50 篇文章 0 订阅

      从Flash Player 10.1 和Adobe AIR 2.0 开始,开发人员就能够全局地捕获未处理异常和错误。然而,我们尽量在异常发生时来做处理。全局处理应该只用于您确实无法用其他方法控制的异步异常,或者未经本地捕获的诊断和日志异常。

     全局异常一个核心类就是UncaughtErrorEvent.UNCAUGHT_ERROR,LoaderInfo对象可以监听该事件,Application当然也可以监听该事件了。

   下面是UncaughtErrorEvent的API的介绍

   

 

 

   以下代码是我写的一个记录全局未捕获异常的代码,仅供参考:

 

package org.sdp.context
{
	import flash.display.LoaderInfo;
	import flash.events.ErrorEvent;
	import flash.events.UncaughtErrorEvent;
	
	import org.sdp.logging.logger.ILogger;
	import org.sdp.logging.manager.LogManager;

	/**
	 * 
	 * desc:捕获未catch的异常信息
	 * 
	 * @author hanjn
	 * @version 1.0.0
	 * @date 2011-8-21
	 */
	public class GlobalErrorHandler
	{
		
		private static var globalErrorHandler:GlobalErrorHandler=null;
		
		private static var log:ILogger=LogManager.registerLog("org.sdp.context.GlobalErrorHandler");
		
		/**
		 * 得到GlobalErrorHandler实例
		 * @return GlobalErrorHandler一个实例
		 * 
		 */
		public static function  getInstance():GlobalErrorHandler {
			if(globalErrorHandler==null){
				globalErrorHandler=new GlobalErrorHandler();
			}
			return globalErrorHandler;
		}
		/**
		 * 对一个LoaderInfo添加Uncaught事件 
		 * @param loaderInfo
		 * 
		 */
		public function addUncaughtEvent(loaderInfo:LoaderInfo):void{
			loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,uncaughtErrorHandler);
		}
		/**
		 *处理caught事件 
		 * @param event
		 * 
		 */
		private function uncaughtErrorHandler(event:UncaughtErrorEvent):void{
			if (event.error is Error)
			{
				var error:Error = event.error as Error;
				log.error("errorId={0}  message={1}\n  stack={2}",error.errorID,error.message,error.getStackTrace());
			}
			else if (event.error is ErrorEvent)
			{
				var errorEvent:ErrorEvent = event.error as ErrorEvent;
				// do something with the error
				log.error("errorId={0}  message={1}\n  stack={2}",[error.errorID,error.message,error.getStackTrace()]);
			}
			else
			{
				// a non-Error, non-ErrorEvent type was thrown and uncaught
				log.error("Uncaught Error!");
			}

		}
	}
}

 

  

 使用方法很简单:

 

 

 

 

 

GlobalErrorHandler.getInstance().addUncaughtEvent(application.loaderInfo);

 

 

 

 

 

 

 

  注意一点上上面的代码只针对Application有效,对ModuleLoader里面加载的module就无效了饿,看了一片文章后,通过下面的代码可以监听Module的异常事件

 

 

<?xml version="1.0" encoding="utf-8"?>
<mx: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"
layout="vertical"
initialize="init()">

<fx:Script>
<![CDATA[
import mx.events.ModuleEvent;

private function init():void
{
systemManager.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR,
uncaughtErrorHandler);
}

private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
trace("caught uncaught error");
event.preventDefault();
}

]]>
</fx:Script>

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<mx:ModuleLoader id="moduleLoader" url="GEHFlexModule.swf" width="200" height="150" />
</mx:Application>

 

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值