异常的传播机制有以下几种:
l 异常自动传播
l 在同一层内部,捕获或者再抛出原有异常
l 捕获,包装和抛出包装后的异常
我们不推荐直接抛出原有异常,因为恶意的用户能够从系统诊断信息中得知应用的详细情况,并从中查找应用的弱点。异常应用程序块提供了一旦配置的Handler执行后,就产生对应的post-handling动作,该动作有如下选项: None - 没有重抛异常的动作。 NotifyRethrow - 告诉调用程序:Policy推荐应该重抛异常。 ThrowNewException - 在所有的Handler执行后,向调用程序抛出最终异常(并不一定是原始的异常)。
可以格式化任何System.Exception类型的异常 能够用来记录或者显示异常的详细信息 字符型格式化器——TextExceptionFormatter:创建在一个屏幕上,日志中或以其他形式表现的,可以表现异常信息的详细记录 XML格式化器——XMLExceptionFormatter:针对一个异常,创建一个用XML表现形式表现记录,每一个异常的属性,均可以被存储为XML元素。 看一下在Enterprise Library Quick Start中提供的自定义的ExceptionFormatter,实现了TextExceptionFormatter类:
三.异常的格式化
1/**//// <summary> 2 /// Summary description for AppTextExceptionFormatter. 3 /// </summary> 4 public class AppTextExceptionFormatter : TextExceptionFormatter 5 { 6 public AppTextExceptionFormatter(TextWriter writer, Exception exception) 7 : base (writer, exception) 8 { 9 } 10 11 protected override void WriteDescription() 12 { 13 // An exception of type {0} occurred and was caught. 14 string line = String.Format("An exception of type {0}
occurred and was caught.", base.Exception.GetType().FullName); 15 this.Writer.WriteLine(line); 16 } 17 18 protected override void WriteExceptionType(Type exceptionType) 19 { 20 base.Indent(); 21 base.Writer.WriteLine("Type : {0}", exceptionType.FullName); 22 } 23 24 public override void Format() 25 { 26 //this.Writer.WriteLine("Message : {0}", message); 27 this.WriteDescription(); 28 //this.WriteExceptionType(base.Exception.GetType()); 29 base.WriteMessage(base.Exception.Message); 30 } 31 32 }
四.创建自定义的异常处理器
异常处理应用程序块允许您包装并使用您自己的例外业务处理流程,例如在时间记录系统中填写一个事件,利用业务规范进行包装和替代,利用另外的记录系统进行记录(比较常用的有Log4net,前段时间深渊野鱼介绍的,还没用过^_^),这种灵活的可配置性,将允许您在不同的异常类型及其策略中灵活的配置。
可以通过实现ExceptionHandler抽象类,来创建定制的Handler
1public abstract class ExceptionHandler : ConfigurationProvider, IExceptionHandler
该抽象类继承ConfigurationProvider类,并实现IExceptionHandler接口。ConfigurationProvider抽象类实现了IConfigurationProvider接口,用来读取配置数据。
1public abstract class ConfigurationProvider : IConfigurationProvider
使用支持序列化的数据类型作为配置参数,还有要注意数据类型的简单,避免“Exception Handling Exceptions”
看一下在Enterprise Library Quick Start中提供了定制Handler的实现:
1/**//// <summary> 2 /// Summary description for GlobalPolicyExceptionHandler. 3 /// </summary> 4 public class AppMessageExceptionHandler : ExceptionHandler 5 { 6 public AppMessageExceptionHandler() 7 { 8 } 9 10 public override void Initialize(ConfigurationView configurationView) 11 { 12 } 13 14 public override Exception HandleException(Exception exception,
string policyName, Guid correlationID) 15 { 16 DialogResult result = this.ShowThreadExceptionDialog(exception); 17 18 // Exits the program when the user clicks Abort. 19 if (result == DialogResult.Abort) 20 Application.Exit(); 21 22 return exception; 23 } 24 25 // Creates the error message and displays it. 26 private DialogResult ShowThreadExceptionDialog(Exception e) 27 { 28 string errorMsg = e.Message + Environment.NewLine + Environment.NewLine; 29 30 return MessageBox.Show(errorMsg, "Application Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop); 31 } 32 }
结束语:异常处理应用程序块的进阶篇就写到这里了。