Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 19
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Unexpected Errors in Managed Applications


Autor: Siggi
Sprache: C#
Bewertung: 8,2
(1 Bewertung)
Anzahl der Aufrufe: 3172
  
Kick it on dotnet-kicks.de  

Beschreibung:

Behandelt alle unbehandelten Exceptions einer Application

Abgelegt unter: Exception.



C#
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// see also
// [Unexpected Errors in Managed Applications]
// http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx
namespace $MyNamespace${
   static class Program {
      public static void Main() {
         // try-catch innerhalb des Main:
         try {
            SetExceptionHandler();
            // Start application logic
            // Perhaps call to Application.Run(...);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
         } catch (Exception e) {
            HandleUnhandledException(e);
         }
         // try-catch innerhalb des Main:
         try {
            SubMain();
         } catch (Exception e) {
            HandleUnhandledException(e);
         }
      }
   
      public static void SetExceptionHandler() {
         // Setup unhandled exception handlers
         AppDomain.CurrentDomain.UnhandledException += // CLR
            new UnhandledExceptionEventHandler(OnUnhandledException);
   
         Application.ThreadException += // Windows Forms
            new System.Threading.ThreadExceptionEventHandler(
                OnGuiUnhandedException);
      }
   
      // CLR unhandled exception
      private static void OnUnhandledException(Object sender,
         UnhandledExceptionEventArgs e) {
         HandleUnhandledException(e.ExceptionObject);
      }
   
      // Windows Forms unhandled exception
      private static void OnGuiUnhandedException(Object sender,
         ThreadExceptionEventArgs e) {
         HandleUnhandledException(e.Exception);
      }
   
      static void HandleUnhandledException(Object o) {
         Exception e = o as Exception;
   
         if (e != null) { // Report System.Exception info
            Debug.WriteLine("Exception = " + e.GetType());
            Debug.WriteLine("Message = " + e.Message);
            Debug.WriteLine("FullText = " + e.ToString());
         } else { // Report exception Object info
            Debug.WriteLine("Exception = " + o.GetType());
            Debug.WriteLine("FullText = " + o.ToString());
         }
   
         MessageBox.Show("An unhandled exception occurred " +
            "and the application is shutting down.");
         Environment.Exit(1); // Shutting down
      }
   }
}

Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.