我自己的异常处理程序仍然抛出异常并使应用程序崩溃
My own exception handler still throwing the exception and crashing the application
Winforms 应用程序。
这是主要的:
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)
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Unhadled domain exception:\n\n" + ex.Message);
Application.Exit();
// It should terminate our main thread so Application.Exit() is unnecessary here
}
private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
{
MessageBox.Show("Unhandled exception catched.\n Application is going to close now.");
// Here we can decide if we want to end our application or do something else
Application.Exit();
}
}
这是我故意生成异常的地方
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int zero = 0;
//int number = 1 / zero;
throw new System.IO.FileNotFoundException();
}
}
堆栈跟踪:
System.IO.FileNotFoundException was unhandled
HResult=-2147024894
Message=Unable to find the specified file.
Source=NewPostSharpSolution
StackTrace:
at NewPostSharpSolution.Form1..ctor() in C:\Users\Joao\Documents\Visual Studio 2015\Projects\NewPostSharpSolution\NewPostSharpSolution\Form1.cs:line 21
at NewPostSharpSolution.Program.Main() in C:\Users\Joao\Documents\Visual Studio 2015\Projects\NewPostSharpSolution\NewPostSharpSolution\Program.cs:line 30
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
处理程序显示异常的消息框,但仍然中断应用程序...
有什么我想念的吗?我以为实现这个处理程序会让我决定如何处理异常??
来自 MSDN AppDomain.UnhandledException Event:
This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.
这意味着 - 您无法处理异常。您可以记录有关异常的信息,显示一些消息等。但是您不能阻止应用程序终止。
虽然最好的做法是在捕获到未处理的异常时终止应用程序(在生成错误报告或其他内容之后),但处理此事件肯定会不会关闭申请。
在您的情况下,应用程序终止是因为您在 Form1
的构造函数中抛出异常,该构造函数在 Main
方法中实例化。 所以最终您的 Main
方法中会出现未处理的异常。
例如在表单的 Load
事件中抛出异常,您的应用程序将不会关闭(除非您将 Application.Exit
行留在那里...)
static class Program
{
[STAThread]
static void Main()
{
Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// throw new Exception("Crash");
}
private void Form1_Load(object sender, EventArgs e)
{
throw new Exception("No crash");
}
}
Winforms 应用程序。 这是主要的:
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)
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show("Unhadled domain exception:\n\n" + ex.Message);
Application.Exit();
// It should terminate our main thread so Application.Exit() is unnecessary here
}
private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
{
MessageBox.Show("Unhandled exception catched.\n Application is going to close now.");
// Here we can decide if we want to end our application or do something else
Application.Exit();
}
}
这是我故意生成异常的地方
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int zero = 0;
//int number = 1 / zero;
throw new System.IO.FileNotFoundException();
}
}
堆栈跟踪:
System.IO.FileNotFoundException was unhandled
HResult=-2147024894
Message=Unable to find the specified file.
Source=NewPostSharpSolution
StackTrace:
at NewPostSharpSolution.Form1..ctor() in C:\Users\Joao\Documents\Visual Studio 2015\Projects\NewPostSharpSolution\NewPostSharpSolution\Form1.cs:line 21
at NewPostSharpSolution.Program.Main() in C:\Users\Joao\Documents\Visual Studio 2015\Projects\NewPostSharpSolution\NewPostSharpSolution\Program.cs:line 30
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
处理程序显示异常的消息框,但仍然中断应用程序...
有什么我想念的吗?我以为实现这个处理程序会让我决定如何处理异常??
来自 MSDN AppDomain.UnhandledException Event:
This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.
这意味着 - 您无法处理异常。您可以记录有关异常的信息,显示一些消息等。但是您不能阻止应用程序终止。
虽然最好的做法是在捕获到未处理的异常时终止应用程序(在生成错误报告或其他内容之后),但处理此事件肯定会不会关闭申请。
在您的情况下,应用程序终止是因为您在 Form1
的构造函数中抛出异常,该构造函数在 Main
方法中实例化。 所以最终您的 Main
方法中会出现未处理的异常。
例如在表单的 Load
事件中抛出异常,您的应用程序将不会关闭(除非您将 Application.Exit
行留在那里...)
static class Program
{
[STAThread]
static void Main()
{
Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// throw new Exception("Crash");
}
private void Form1_Load(object sender, EventArgs e)
{
throw new Exception("No crash");
}
}