C# 4.0 如何在c#中捕获未处理的异常?
我试图捕获程序中任何未处理的异常,我在程序主类中使用此代码C# 4.0 如何在c#中捕获未处理的异常?,c#-4.0,exception-handling,error-handling,c#-3.0,C# 4.0,Exception Handling,Error Handling,C# 3.0,我试图捕获程序中任何未处理的异常,我在程序主类中使用此代码 static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefau
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();
}
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException+=新的UnhandledExceptionEventHandler(CurrentDomain\u UnhandledException);
Application.Run(新Form1());
}
public static void CurrentDomain_UnhandledException(对象发送方,UnhandledExceptionEventArgs e)
{
Show((例如,ExceptionObject作为Exception.Message,“未处理的域异常”);
Application.Exit();
}
现在在我的表单form1中,我尝试创建一个简单的异常:除以零,没有try-catch块,主模块中的代码确实会截获异常,但我仍然有MS Visual studio对话框。应用程序不会退出。当然,在实际情况下,我会记录/发送错误。但我想了解执行的原因截获我的异常后继续?谢谢从主UI线程捕获所有异常对我有用:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Add handler for UI thread exceptions
Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
// Force all WinForms errors to go through handler
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// This handler is for catching non-UI thread exceptions
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new Form1());
}
private static void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Unhadled domain exception:\n\n" + ex.Message);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal exception happend inside UnhadledExceptionHandler: \n\n"
+ exc.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
// It should terminate our main thread so Application.Exit() is unnecessary here
}
private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
{
try
{
MessageBox.Show("Unhandled exception catched.\n Application is going to close now.");
}
catch
{
try
{
MessageBox.Show("Fatal exception happend inside UIThreadException handler",
"Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
// Here we can decide if we want to end our application or do something else
Application.Exit();
}
}
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//为UI线程异常添加处理程序
Application.ThreadException+=新的ThreadExceptionEventHandler(UIThreadException);
//强制所有WinForms错误通过处理程序
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//此处理程序用于捕获非UI线程异常
AppDomain.CurrentDomain.UnhandledException+=新的UnhandledExceptionEventHandler(CurrentDomain\u UnhandledException);
Application.Run(新Form1());
}
私有静态void CurrentDomain_UnhandledException(对象发送方,UnhandledExceptionEventArgs e)
{
尝试
{
异常ex=(异常)e.ExceptionObject;
Show(“未处理的域异常:\n\n”+ex.Message);
}
捕获(异常exc)
{
尝试
{
MessageBox.Show(“UnhadledExceptionHandler中发生了致命异常:\n\n”
+exc.Message“”,MessageBoxButtons.OK,MessageBoxIcon.Stop);
}
最后
{
Application.Exit();
}
}
//它应该终止我们的主线程,所以这里不需要Application.Exit()
}
私有静态无效UIThreadException(对象发送方,ThreadExceptionEventArgs t)
{
尝试
{
Show(“捕获到未处理的异常。\n应用程序将立即关闭”);
}
抓住
{
尝试
{
Show(“UIThreadException处理程序内部发生致命异常”,
“致命Windows窗体错误”,MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Stop);
}
最后
{
Application.Exit();
}
}
//在这里,我们可以决定是结束应用程序还是执行其他操作
Application.Exit();
}
}
而不是
Application.Exit()
您不必使用Application.Exit或Environment.Exit,这只是VisualStudio不断拦截导致一系列未处理异常的行为,但实际上您的应用程序将崩溃(退出)如果设置了isTerminating标志,并且这是正确使用它的方法,我建议不要为此使用任何用户界面,也不要在这个事件中使用任何其他需要资源的进程,静默日志就可以了 此事件的目的是获取日志,了解事件发生的确切原因,这并不是在应用程序崩溃时让应用程序看起来很好,或者就此向用户道歉。但是,当用户下次登录时,您将有机会执行上述操作 因此,即使您计划通过电子邮件发送异常日志,最好的方法是编写该文件,并在下次启动应用程序时通过电子邮件发送该文件 我通常使用log4net并使用以下事件
[STAThread]
static void Main()
{
log.Info("Starting deployer application");
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
Application.Run(ConnectionForm.GetInstance);
}
static void Application_ApplicationExit(object sender, EventArgs e)
{
log.Info("Application Closed");
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
log.Error(string.Format("*** UNHANDLED APPDOMAIN EXCEPTION ({0}) *****", e.IsTerminating ? "Terminating" : "Non-Terminating"), e.ExceptionObject as Exception);
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
log.Error("*** UNHANDLED THREAD EXCEPTION *****", e.Exception);
}
如果您以前从未使用过log4net。请使用Environment.Exit()而不是Application.Exit()。
[STAThread]
static void Main()
{
log.Info("Starting deployer application");
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
Application.Run(ConnectionForm.GetInstance);
}
static void Application_ApplicationExit(object sender, EventArgs e)
{
log.Info("Application Closed");
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
log.Error(string.Format("*** UNHANDLED APPDOMAIN EXCEPTION ({0}) *****", e.IsTerminating ? "Terminating" : "Non-Terminating"), e.ExceptionObject as Exception);
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
log.Error("*** UNHANDLED THREAD EXCEPTION *****", e.Exception);
}