Handle uncatched exception in Windows Form Application

The core code is:
Application.ThreadException  +=  
 
new  ThreadExceptionEventHandler( ApplicatioThreadExceptionHandler); 

Two notes:

1. The handler needs to be set up BEFORE the Windows message loop is constructed.
This means it needs to be before Application.Run(MainForm). If you are showing a
splash screen at start up, or even a ‘Do you want to upgrade to the new version?’
form before calling Application.Run, you need to put your
Application.ThreadException += new ThreadExceptionEventHandler(… statement
before the code to do those things is called.

2. The handler is not in force until the initialization of your main form has completed.
Any exception in the constructor of the main form, or in any code you execute prior
to Application.Run(MainForm) will not be handled by the top-level handler. Of course,
putting complex initialization code in the constructor of a form is a bad idea in general,
 not least because it gets run by the designer when you open the form in Visual Studio.
However, this does mean that you may need a try..catch block around any initialization code
 that runs before the main form is launched in case it throws an exception.

Referred from Top-level Exception Handling in Windows Forms Applications

转载于:https://www.cnblogs.com/easoney/archive/2007/11/27/974018.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值