WebCast听课录(10)

课程名: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来捕获所有未处理的主线程中出现的异常,但并不能处理你自己创建的工作进程中出现的异常。

None.gif public static void Main( string []args)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifApplication.ThreadException
+=new
InBlock.gifThreadExceptionEventHandler(
InBlock.gifMainUIThreadExceptionHandler);
InBlock.gif
InBlock.gif
InBlock.gifDemo6();
InBlock.gif
InBlock.gifSystem.Windows.Forms.Application.Run(
InBlock.gif
newNorthwindMDIMain());
ExpandedBlockEnd.gif}

None.gif
None.gif
public static void MainUIThreadExceptionHandler(
None.gifExceptione)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifMainUIThreadExceptionHandler(
null,newSystem.Threading.
InBlock.gifThreadExceptionEventArgs(e));
ExpandedBlockEnd.gif}

None.gif
None.gif
public static void MainUIThreadExceptionHandler( object
None.gifsender,ThreadExceptionEventArgst)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifExceptionManager.Publish(t.Exception);
InBlock.gif
InBlock.gif
//Telltheuseraboo-boooccurreddot.gif
InBlock.gif
MessageBox.Show(
InBlock.gif
"NorthwindOrdershasencounteredthefollowing"
InBlock.gif
+"problem:"+(char)13
InBlock.gif
+(char)13+t.Exception.Message
InBlock.gif
+(char)13+(char)13
InBlock.gif
+"Pleasecontactyouradministratororthe"
InBlock.gif
+"NorthwindHelpdeskat"
InBlock.gif
+(char)13+(char)13+"1-855-555-5555."
InBlock.gif
+(char)13+(char)13
InBlock.gif
+"Weapologizefortheinconvenience."
InBlock.gif,
"NorthwindOrdersEncounteredaProblem"
InBlock.gif,MessageBoxButtons.OK
InBlock.gif,MessageBoxIcon.Error
InBlock.gif,MessageBoxDefaultButton.Button1
InBlock.gif,MessageBoxOptions.ServiceNotification);
ExpandedBlockEnd.gif}

None.gif

5,必须考虑在工作线程中出现的异常,在线程的入口使用try-catch,并且使用delegate等方式将异常通知给主线程,此外,线程之间要访问对方的界面成员时,应该通过BeginInvoke()方法来进行。


None.gif public delegate void DoneDelegate( bool Stopped);
None.gif
public delegate void
None.gifWorkerThreadExceptionHandlerDelegate(Exceptione);
None.gif
void WorkerThreadExceptionHandler(Exceptione)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifUpdateProgress();
InBlock.gifSetStatusText(
"ProblemEncountered");
InBlock.gif
this.Text="Customers";
InBlock.gifbtnSave.Text
="Save";
InBlock.gifbtnClose.Enabled
=true;
InBlock.gif
InBlock.gif
//Calltheglobalexceptionhandler
InBlock.gif
MainClass.MainUIThreadExceptionHandler(
InBlock.gif
this,newSystem.Threading.
InBlock.gifThreadExceptionEventArgs(e));
ExpandedBlockEnd.gif}

None.gif
private void btnSave_Click( object sender,System.EventArgse)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(true==btnSave.Enabled)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.Text="Customers(Savingdot.gif)";
InBlock.gifbtnSave.Enabled
=false;
InBlock.gifbtnClose.Enabled
=false;
InBlock.gifSystem.Threading.Threadt
=
InBlock.gif
newSystem.Threading.Thread(
InBlock.gif
newSystem.Threading.ThreadStart(
InBlock.gifSaveCustomer));
InBlock.gift.Start();
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifisCanceled
=true;
InBlock.gifbtnSave.Enabled
=true;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch(NotSupportedExceptionexception)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox.Show(
"Codenotdoneyet"
InBlock.gif
+exception.Message
InBlock.gif);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exceptionexception)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Telltheuseraboo-boooccurreddot.gif
InBlock.gif
MainClass.MainUIThreadExceptionHandler(this
InBlock.gif,
newSystem.Threading.
InBlock.gifThreadExceptionEventArgs(exception));
InBlock.gifMessageBox.Show(
InBlock.gif
"NorthwindOrdershasencounteredaproblem."
InBlock.gif
+"Pleasecontactyouradministratororthe"
InBlock.gif
+"NorthwindHelpdeskat"
InBlock.gif
+(char)13+(char)13+"1-855-555-5555."
InBlock.gif
+(char)13+(char)13
InBlock.gif
+"Weapologizefortheinconvenience."
InBlock.gif,
"NorthwindOrdersEncounteredaProblem"
InBlock.gif,MessageBoxButtons.OK
InBlock.gif,MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif}

InBlock.gif
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.Cursor=Cursors.Default;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
private void SaveCustomer()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSetStatusText(
InBlock.gif
"Savingrecordonabackgroundthreaddot.gif");
InBlock.gif
InBlock.gif
//TODO:Addcodetosavethecustomerrecord
InBlock.gif
this.BindingContext[dsCustomers,"Customers"].
InBlock.gifEndCurrentEdit();
InBlock.gifdsCustomersobjDataSetChanges
=((dsCustomers)
InBlock.gif(dsCustomers.GetChanges()));
InBlock.gif
if(null!=objDataSetChanges)
InBlock.gif
newNorthwindDataAccess().UpdateCustomers(
InBlock.gifobjDataSetChanges);
InBlock.gif
InBlock.gifisCanceled
=false;
InBlock.gifDoneDelegatedelDone
=newDoneDelegate(Done);
InBlock.gifdelDone.BeginInvoke(isCanceled,
null,null);
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exceptione)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
newWorkerThreadExceptionHandlerDelegate(
InBlock.gifWorkerThreadExceptionHandler).BeginInvoke(e
InBlock.gif,
null
InBlock.gif,
null);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
private void Done( bool Stopped)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifUpdateProgress();
InBlock.gif
this.Text="Customers";
InBlock.gifbtnSave.Enabled
=true;
InBlock.gifbtnClose.Enabled
=true;
InBlock.gif
if(Stopped)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox.Show(
"SaveStopped"
InBlock.gif,
"Stopped"
InBlock.gif,MessageBoxButtons.OK
InBlock.gif,MessageBoxIcon.Warning);
InBlock.gifSetStatusText(
"SaveStopped");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox.Show(
"SaveDone"
InBlock.gif,
"Done"
InBlock.gif,MessageBoxButtons.OK
InBlock.gif,MessageBoxIcon.Information);
InBlock.gifSetStatusText(
"SaveDone");
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

6,在构造函数中不要加try-catch,因为这会把异常再抛给外部方法,还不如不处理,直接抛给外部好了。

7,异常处理程序块采用的是publisher/subscriber设计模式,在工程中添加对它的引用,并引入名称空间Microsoft.ApplicationBlocks.ExceptionManagement就可以使用ExceptionManager.Publish()来进行异常的发布。在app.config中可以对异常管理进行启/停,还可以添加自己的异常处理模块。

None.gif <? xmlversion="1.0"encoding="utf-8" ?>
None.gif
< configuration >
None.gif
< configSections >
None.gif
< section name ="exceptionManagement"
None.giftype
="Microsoft.ApplicationBlocks.ExceptionManagement.ExceptionManagerSectionHandler,Microsoft.ApplicationBlocks.ExceptionManagement" />
None.gif
</ configSections >
None.gif
None.gif
< exceptionManagement mode ="on" >
None.gif
< publisher mode ="on" assembly ="Microsoft.ApplicationBlocks.ExceptionManagement"
None.giftype
="Microsoft.ApplicationBlocks.ExceptionManagement.DefaultPublisher"
None.giflogname
="NorthwindExceptionsLog"
None.gifapplicationname
="NorthwindTradersOrders" />
None.gif
</ exceptionManagement >
None.gif
None.gif
</ configuration >
None.gif

None.gif public static void MainUIThreadExceptionHandler( object
None.gifsender,ThreadExceptionEventArgst)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifExceptionManager.Publish(t.Exception);
InBlock.gif
InBlock.gif
//Telltheuseraboo-boooccurreddot.gif
InBlock.gif
MessageBox.Show(
InBlock.gif
"NorthwindOrdershasencounteredthefollowing"
InBlock.gif
+"problem:"+(char)13
InBlock.gif
+(char)13+t.Exception.Message
InBlock.gif
+(char)13+(char)13
InBlock.gif
+"Pleasecontactyouradministratororthe"
InBlock.gif
+"NorthwindHelpdeskat"
InBlock.gif
+(char)13+(char)13+"1-855-555-5555."
InBlock.gif
+(char)13+(char)13
InBlock.gif
+"Weapologizefortheinconvenience."
InBlock.gif,
"NorthwindOrdersEncounteredaProblem"
InBlock.gif,MessageBoxButtons.OK
InBlock.gif,MessageBoxIcon.Error
InBlock.gif,MessageBoxDefaultButton.Button1
InBlock.gif,MessageBoxOptions.ServiceNotification);
ExpandedBlockEnd.gif}

None.gif

可以在事件查看器中看到异常已经发布进来了:

2006103101.JPG

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值