相关疑难解决方法(0)

如何在c#中捕获未处理的异常?

我试图在我的程序中捕获任何unhandeled Exception,我在Program主类中使用此代码

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();
}
Run Code Online (Sandbox Code Playgroud)

现在我的表单form1,我尝试创建一个简单的异常:除以零,没有try catch块,主模块中的代码确实拦截了异常,但我仍然有MS Visual studio对话框.应用程序不会退出.当然在实际情况下,我会记录/邮寄错误.但我想理解为什么在我拦截我的异常后继续执行?谢谢

error-handling exception-handling c#-3.0 c#-4.0

4
推荐指数
1
解决办法
1万
查看次数