课程名:Windows应用程序开发入门到精通七:优化.NET异常处理<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1.多个catch块的情况时会过滤异常,先截获具体的异常,再截获一般的异常,并且这是从编译器的层次就支持的。
2.应该从系统异常(System.ApplicationException)派生自己的具体异常,从而为特定的应用程序提供更好的支持。
3.异常处理技术:1)记录异常:在文件中记录异常;在数据库中记录;在Eventlog中记录。2)发email等信息来通知异常。总之,要以用户友好的方式通知异常的发生。
4.使用Application对象中的ThreadException属性来设置一个delegate来捕获所有未处理的主线程中出现的异常,但并不能处理你自己创建的工作进程中出现的异常。
{
Application.ThreadException+=new
ThreadExceptionEventHandler(
MainUIThreadExceptionHandler);
Demo6();
System.Windows.Forms.Application.Run(
newNorthwindMDIMain());
}
public static void MainUIThreadExceptionHandler(
Exceptione)
{
MainUIThreadExceptionHandler(null,newSystem.Threading.
ThreadExceptionEventArgs(e));
}
public static void MainUIThreadExceptionHandler( object
sender,ThreadExceptionEventArgst)
{
ExceptionManager.Publish(t.Exception);
//Telltheuseraboo-boooccurred
MessageBox.Show(
"NorthwindOrdershasencounteredthefollowing"
+"problem:"+(char)13
+(char)13+t.Exception.Message
+(char)13+(char)13
+"Pleasecontactyouradministratororthe"
+"NorthwindHelpdeskat"
+(char)13+(char)13+"1-855-555-5555."
+(char)13+(char)13
+"Weapologizefortheinconvenience."
,"NorthwindOrdersEncounteredaProblem"
,MessageBoxButtons.OK
,MessageBoxIcon.Error
,MessageBoxDefaultButton.Button1
,MessageBoxOptions.ServiceNotification);
}
5,必须考虑在工作线程中出现的异常,在线程的入口使用try-catch,并且使用delegate等方式将异常通知给主线程,此外,线程之间要访问对方的界面成员时,应该通过BeginInvoke()方法来进行。
public delegate void
WorkerThreadExceptionHandlerDelegate(Exceptione);
void WorkerThreadExceptionHandler(Exceptione)
{
UpdateProgress();
SetStatusText("ProblemEncountered");
this.Text="Customers";
btnSave.Text="Save";
btnClose.Enabled=true;
//Calltheglobalexceptionhandler
MainClass.MainUIThreadExceptionHandler(
this,newSystem.Threading.
ThreadExceptionEventArgs(e));
}
private void btnSave_Click( object sender,System.EventArgse)
{
try
{
if(true==btnSave.Enabled)
{
this.Text="Customers(Saving)";
btnSave.Enabled=false;
btnClose.Enabled=false;
System.Threading.Threadt=
newSystem.Threading.Thread(
newSystem.Threading.ThreadStart(
SaveCustomer));
t.Start();
}
else
{
isCanceled=true;
btnSave.Enabled=true;
}
}
catch(NotSupportedExceptionexception)
{
MessageBox.Show("Codenotdoneyet"
+exception.Message
);
}
catch(Exceptionexception)
{
//Telltheuseraboo-boooccurred
MainClass.MainUIThreadExceptionHandler(this
,newSystem.Threading.
ThreadExceptionEventArgs(exception));
MessageBox.Show(
"NorthwindOrdershasencounteredaproblem."
+"Pleasecontactyouradministratororthe"
+"NorthwindHelpdeskat"
+(char)13+(char)13+"1-855-555-5555."
+(char)13+(char)13
+"Weapologizefortheinconvenience."
,"NorthwindOrdersEncounteredaProblem"
,MessageBoxButtons.OK
,MessageBoxIcon.Error);
}
finally
{
this.Cursor=Cursors.Default;
}
}
private void SaveCustomer()
{
try
{
SetStatusText(
"Savingrecordonabackgroundthread");
//TODO:Addcodetosavethecustomerrecord
this.BindingContext[dsCustomers,"Customers"].
EndCurrentEdit();
dsCustomersobjDataSetChanges=((dsCustomers)
(dsCustomers.GetChanges()));
if(null!=objDataSetChanges)
newNorthwindDataAccess().UpdateCustomers(
objDataSetChanges);
isCanceled=false;
DoneDelegatedelDone=newDoneDelegate(Done);
delDone.BeginInvoke(isCanceled,null,null);
}
catch(Exceptione)
{
newWorkerThreadExceptionHandlerDelegate(
WorkerThreadExceptionHandler).BeginInvoke(e
,null
,null);
}
}
private void Done( bool Stopped)
{
UpdateProgress();
this.Text="Customers";
btnSave.Enabled=true;
btnClose.Enabled=true;
if(Stopped)
{
MessageBox.Show("SaveStopped"
,"Stopped"
,MessageBoxButtons.OK
,MessageBoxIcon.Warning);
SetStatusText("SaveStopped");
}
else
{
MessageBox.Show("SaveDone"
,"Done"
,MessageBoxButtons.OK
,MessageBoxIcon.Information);
SetStatusText("SaveDone");
}
}
6,在构造函数中不要加try-catch,因为这会把异常再抛给外部方法,还不如不处理,直接抛给外部好了。
7,异常处理程序块采用的是publisher/subscriber设计模式,在工程中添加对它的引用,并引入名称空间Microsoft.ApplicationBlocks.ExceptionManagement就可以使用ExceptionManager.Publish()来进行异常的发布。在app.config中可以对异常管理进行启/停,还可以添加自己的异常处理模块。
< configuration >
< configSections >
< section name ="exceptionManagement"
type ="Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,Microsoft.ApplicationBlocks.ExceptionManagement" />
</ configSections >
< exceptionManagement mode ="on" >
< publisher mode ="on" assembly ="Microsoft.ApplicationBlocks.ExceptionManagement"
type ="Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher"
logname ="NorthwindExceptionsLog"
applicationname ="NorthwindTradersOrders" />
</ exceptionManagement >
</ configuration >
sender,ThreadExceptionEventArgst)
{
ExceptionManager.Publish(t.Exception);
//Telltheuseraboo-boooccurred
MessageBox.Show(
"NorthwindOrdershasencounteredthefollowing"
+"problem:"+(char)13
+(char)13+t.Exception.Message
+(char)13+(char)13
+"Pleasecontactyouradministratororthe"
+"NorthwindHelpdeskat"
+(char)13+(char)13+"1-855-555-5555."
+(char)13+(char)13
+"Weapologizefortheinconvenience."
,"NorthwindOrdersEncounteredaProblem"
,MessageBoxButtons.OK
,MessageBoxIcon.Error
,MessageBoxDefaultButton.Button1
,MessageBoxOptions.ServiceNotification);
}
可以在“事件查看器”中看到异常已经发布进来了: