C# 我自己的异常处理程序仍在抛出异常并使应用程序崩溃
Winforms应用程序。 这是主要问题:C# 我自己的异常处理程序仍在抛出异常并使应用程序崩溃,c#,winforms,exception-handling,handler,C#,Winforms,Exception Handling,Handler,Winforms应用程序。 这是主要问题: static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Applicati
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();
}
}
堆栈跟踪:
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:
处理程序显示异常的messagebox,但仍会中断应用程序
我有什么遗漏吗?我认为实现这个处理程序可以让我决定如何处理MSDN中的异常:
此事件提供未捕获异常的通知。它允许应用程序在系统默认处理程序向用户报告异常并终止应用程序之前记录有关异常的信息
这意味着-您无法处理异常。您可以记录有关异常的信息、显示一些消息等,但不能阻止应用程序终止。尽管在捕获未处理的异常时(生成错误报告或其他信息后),终止应用程序是最佳做法,处理此事件肯定不会关闭应用程序 在您的例子中,应用程序被终止,因为您在
Form1
的构造函数中抛出了一个异常,该异常在Main
方法中实例化。因此,最终在Main
方法中会出现一个未处理的异常
例如,在表单的Load
事件中抛出异常,您的应用程序将不会关闭(除非您离开应用程序。退出那里的行…)
为什么您要尝试捕获消息框。显示??是时候清理您的尝试/捕获了。我知道为什么它们都在那里,但如果你现在不这样做,那么排除故障将变得更加困难。您是否介意删除所有日志,然后发布堆栈跟踪?编辑了主日志。因此这与尝试捕获不同?你有解决办法吗?@JoaoVitor No。它主要用于测井目的。如果应用程序崩溃(即发生了未经处理的异常),这是最后一次保存某些信息的机会,这将有助于您以后定义异常的原因。无论如何,应用程序都将处于未确定状态,即使存在阻止其终止的选项。因此,没有必要让崩溃的东西继续存在。我可以让它正常退出,而不在windows“appname.exe已崩溃”或类似的内容中发出该消息吗?@JoaoVitor,但您的应用程序已崩溃。没有借口。如果要阻止该消息,应尽早处理异常。如果您正在获取该文件,请检查它是否存在,或者将该代码包装到try。。catch
为了防止应用程序崩溃,应用程序崩溃是因为OP在表单的构造函数中抛出异常,该异常在Main
方法中实例化。
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:
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");
}
}