我有一个使用C#
编写的WinForm
应用程序,在程序入口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
引用)。令我惊讶的是,这个异常没有创建错误日志文件。现在,这篇文章表明可能是由于
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
为什么会这样呢?
ThreadException
吗? - Ian