Hallo Ralf,
ok, dann haben wir uns wohl mißverstanden.
Ich bezog mich ja auch generell auf eine Änderung in der Main-Methode, d.h. auch das Hinzufügen von
C#-Quelltext
1:
| Application.ThreadException += Application_ThreadException; |
sollte ja in der Main-Methode (d.h. vor Application.Run(...)) passieren.
Ich hatte mal Source-Code bekommen, wo jemand grundsätzlich in alle EventHandler ein
C#-Quelltext
1: 2: 3: 4: 5: 6: 7:
| try { ... } catch { } |
eingebaut hatte (d.h. wirklich alle Exceptions gefangen,
ohne sie zu behandeln, d.h. das Programm lief einfach "munter" weiter...)!!!
(und das selbst noch bei Portierung auf 2.0!), so daß ich in Bezug auf (falsches) ExceptionHandling allergisch reagiere...
Dein Satz
| Ralf Jansen hat folgendes geschrieben: |
... wird eine Exception bereits in Application.Run gefangen ...
|
liest sich, als ob es gar keine Möglichkeit gibt, die Exception selber zu behandeln (daher mein Hinweis auf Application.ThreadException und AppDomain.UnhandledException).
P.S. Ein try/catch um Application.Run ist z.B. dann sinnvoll, um Exceptions zubehandeln, welche im Konstruktor der aufgerufenen MainForm auftreten, z.B.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
| namespace TestApp { static class Program { [STAThread] static void Main() { try { Application.ThreadException += Application_ThreadException;
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } catch (Exception e) { MessageBox.Show(e.Message, "Main Error"); } }
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { MessageBox.Show(e.Exception.Message, "Thread Exception Error"); } } }
namespace TestApp { public partial class TestForm : Form { public TestForm() { InitializeComponent();
throw new Exception("Exception"); } } } |
Ohne den try/catch in der Main-Methode käme dann der tolle Windows-Dialog "TestApp hat ein Problem festgestellt und muß beendet werden!".
Sobald jedoch die Run-Methode läuft, so werden dann wirklich alle Exceptions (des Mainthreads) an Application.ThreadException weitergeleitet.