Ich habe eine WinForm
Anwendung in C#
geschrieben, wo ich einen try-catch
Block in der Program.cs
setzen, in dem Programmeintrag, die static void Main
Methode, gleich zu Beginn der Anwendung wie folgt aus:Kann C# WinForm static void Haupt nicht abfangen Ausnahme?
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());
}
}
}
}
Wie Sie sehen können , die Application
wird in einen try-catch
Block gesteckt und im catch
Block ist das einzige, was es tut, eine Fehlerprotokolldatei zu erstellen.
Jetzt, so weit so gut. Meine Anwendung läuft gut und wenn ein Absturz auftritt, sollte der letzte Exception
vom try-catch
Block erfasst und in der Fehlerprotokolldatei gespeichert werden.
Allerdings, wie ich mein Programm für eine Weile ausführen, bekomme ich eine unbehandelte Ausnahme (null
Referenz). Was mich überrascht ist, dass die Ausnahme keine Fehlerprotokolldatei erstellt.
Nun this post zeigt, dass es möglicherweise durch ThreadException
oder HandleProcessCorruptedStateExceptions
(die beiden upvoted Antworten) verursacht wird, aber mein Fall zeigt eine einfache null
Ausnahme Referenz:
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
Warum das wäre?
So erstellen Sie keinen globalen Ausnahme-Handler. Suchen Sie rechts im Bereich "Verknüpft" nach dieser Seite. Die angenommene Antwort sagt dir was zu tun ist. – jmcilhinney
@jmcilhinney meinst du 'ThreadException'? – Ian
In dem Beispiel gibt es // Starten Sie einen neuen Thread, getrennt von Windows Forms, der eine Ausnahme auslöst. 'private void button2_Click (Objekt Absender, System.EventArgs e) { ThreadStart newThreadStart = neue ThreadStart (newThread_Execute); newThread = neuer Thread (newThreadStart); newThread.Start(); } 'die absichtlich Ausnahme in einem neuen Thread behandeln. Aber kann das eine 'Null'-Referenz-Ausnahme (wie in meinem Fall) anstelle von 'ThreadException' erzeugen (soll dieser Typ sein - oder?)? – Ian