Auf Englisch lesen

Freigeben über


ThreadExceptionEventArgs Klasse

Definition

Stellt Daten für das ThreadException-Ereignis bereit.

C#
public class ThreadExceptionEventArgs : EventArgs
Vererbung
ThreadExceptionEventArgs
Abgeleitet

Beispiele

Im folgenden Beispiel können Sie ein ThreadException Ereignis auslösen, indem Sie auf ein Formular klicken button1 . Im Beispiel werden zwei Klassen erstellt. Die ErrorHandler -Klasse erstellt das Formular und die Schaltfläche, die das Ereignis auslöst. Die CustomExceptionHandler -Klasse stellt die Methoden zum Behandeln der Ausnahme bereit.

In Main der ErrorHandler -Klasse erstellt der Code eine neue instance der Ausnahmebehandlungsklasse, d. h. eine instance von CustomExceptionHandler. Anschließend wird der instance dem Ereignis hinzugefügt, und die Anwendung wird ausgeführt.

In der OnThreadException -Methode in der CustomExceptionHandler -Klasse verwendet das Beispiel eine try...catch...finally -Anweisung, um die Ausnahme zu verarbeiten. Die ShowThreadExceptionDialog -Methode erstellt die anzuzeigende Nachricht und zeigt sie in einem Meldungsfeld an.

C#
using System;
using System.Threading;
using System.Windows.Forms;

// Create a form with a button that, when clicked, throws an exception.
 public class ErrorForm : System.Windows.Forms.Form
 {
    internal Button button1;

    public ErrorForm() : base()
    {
       // Add the button to the form.
       this.button1 = new System.Windows.Forms.Button();
       this.SuspendLayout();
       this.button1.Location = new System.Drawing.Point(100, 43);
       this.button1.Size = new System.Drawing.Size(75, 23);
       this.button1.Text = "Click!";
       this.Controls.Add(this.button1);
       this.button1.Click += new EventHandler(this.button1_Click);

       this.Text = "ThreadException";
       this.ResumeLayout(false);
    }

    // Throw an exception when the button is clicked.
    private void button1_Click(object sender, System.EventArgs e)
    {
       throw new ArgumentException("The parameter was invalid");
    }

    public static void Main()
    {
       // Add the event handler.
       Application.ThreadException += new ThreadExceptionEventHandler(CustomExceptionHandler.OnThreadException);

       // Start the example.
       Application.Run(new ErrorForm());
    }
 }

 // Create a class to handle the exception event.
 internal class CustomExceptionHandler
 {
     // Handle the exception event
    public static void OnThreadException(object sender, ThreadExceptionEventArgs t)
    {
       DialogResult result = ShowThreadExceptionDialog(t.Exception);

       // Exit the program when the user clicks Abort.
       if (result == DialogResult.Abort)
          Application.Exit();
    }

    // Create and display the error message.
    private static DialogResult ShowThreadExceptionDialog(Exception e)
    {
       string errorMsg = "An error occurred.  Please contact the adminstrator " +
            "with the following information:\n\n";
       errorMsg += string.Format("Exception Type: {0}\n\n", e.GetType().Name);
       errorMsg += "\n\nStack Trace:\n" + e.StackTrace;
       return MessageBox.Show(errorMsg, "Application Error",
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    }
 }

Hinweise

Ein ThreadExceptionEventArgs wird von einem Thread erstellt, wenn eine nicht behandelte Ausnahme auftritt. ThreadExceptionEventArgs enthält die Exception aufgetretene.

Konstruktoren

ThreadExceptionEventArgs(Exception)

Initialisiert eine neue Instanz der ThreadExceptionEventArgs-Klasse.

Eigenschaften

Exception

Ruft die aufgetretene Exception ab.

Methoden

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Gilt für:

Produkt Versionen
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Weitere Informationen