我想捕捉任何异常unhandeled在我的计划,我在程序主类中使用此代码如何在c#中捕捉未处理的异常?
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new Form1());
}
public static void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled domain Exception");
Application.Exit();
}
现在即时通讯我的窗体Form1,我试图创建一个简单的异常:除以零,无一个try catch块,主模块中的代码实际上拦截了异常,但我仍然有MS Visual studio对话框。应用程序不会退出。当然在实际情况下,我会记录/邮寄错误。但我想知道为什么执行后我拦截我的异常继续?感谢
使用Environment.Exit()而不是Application.Exit()。 –