【发布时间】:2016-05-25 05:07:23
【问题描述】:
我有一个WinForm 应用程序,用C# 编写,我在Program.cs 中放置了一个try-catch 块,在程序条目中,static void Main 方法,就在应用程序的开头,如下所示:
using System;
using System.IO;
using System.Windows.Forms;
namespace T5ShortestTime {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
try {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new T5ShortestTimeForm());
} catch (Exception e) {
string errordir = Path.Combine(Application.StartupPath, "errorlog");
string errorlog = Path.Combine(errordir, DateTime.Now.ToString("yyyyMMdd_HHmmss_fff") + ".txt");
if (!Directory.Exists(errordir))
Directory.CreateDirectory(errordir);
File.WriteAllText(errorlog, e.ToString());
}
}
}
}
如您所见,Application 被放入 try-catch 块和 catch 块中,它唯一做的就是创建一个错误日志文件。
现在,到目前为止一切顺利。我的应用程序运行良好,如果我遇到崩溃,最后一个Exception 应该被try-catch 块捕获并存储在错误日志文件中。
但是,当我运行我的程序一段时间后,我得到了一个未处理的异常(null 参考)。令我惊讶的是,该异常并没有创建错误日志文件。
现在,this post 表明它可能是由 ThreadException 或 HandleProcessCorruptedStateExceptions(两个最受好评的答案)引起的,但我的案例显示了一个简单的 null 引用异常:
Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: T5ShortestTime.exe
Problem Signature 02: 2.8.3.1
Problem Signature 03: 5743e646
Problem Signature 04: T5ShortestTime
Problem Signature 05: 2.8.3.1
Problem Signature 06: 5743e646
Problem Signature 07: 182
Problem Signature 08: 1b
Problem Signature 09: System.NullReferenceException
OS Version: 6.3.9600.2.0.0.272.7
Locale ID: 1033
Additional Information 1: bb91
Additional Information 2: bb91a371df830534902ec94577ebb4a3
Additional Information 3: aba1
Additional Information 4: aba1ed7202d796d19b974eec93d89ec2
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=280262
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt
为什么会这样?
【问题讨论】:
-
这不是您创建全局异常处理程序的方式。查看此页面右侧的“已链接”部分。那里接受的答案会告诉你该怎么做。
-
@jmcilhinney 你的意思是
ThreadException? -
在示例中, // 启动一个新线程,与 Windows 窗体分开,这将引发异常。
private void button2_Click(object sender, System.EventArgs e) { ThreadStart newThreadStart = new ThreadStart(newThread_Execute); newThread = new Thread(newThreadStart); newThread.Start(); }故意在新线程中处理异常。但这可以创建null引用异常(如我的情况)而不是ThreadException(应该是这种类型 - 不是吗?)?