Capturer toutes les exceptions

De Banane Atomic
Aller à la navigationAller à la recherche

Application Console

Csharp.svg
public static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

// Capture les exceptions non gérées du thread courant.
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception ex = (Exception)e.ExceptionObject;
    Console.WriteLine($"Fatal Error\n{ex.Message}\n{ex.StackTrace}");
    Environment.Exit(1);
}

Application

Csharp.svg
static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += 
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
}

// Capture les exceptions non-gérées du thread graphique.
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    DialogResult result = DialogResult.Abort;
    try
    {
        result = MessageBox.Show("Le logiciel a rencontré l'erreur suivante:\n\n" + 
            e.Exception.Message + e.Exception.StackTrace, 
            "Application Error",
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    }
    finally
    {
        if (result == DialogResult.Abort)
        {
            Application.Exit();
        }
    }
}

// Capture les exceptions non gérées du thread courant.
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    try
    {
        Exception ex = (Exception)e.ExceptionObject;
        MessageBox.Show(ex.Message + ex.StackTrace, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
    finally
    {
        Environment.Exit(-1);
    }
}

WPF

App.xaml
<Application DispatcherUnhandledException="Application_DispatcherUnhandledException">
App.xaml.cs
public partial class App : Application
{
    private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        // Process unhandled exception
        var ex = e.Exception;

        // Prevent default unhandled exception processing
        e.Handled = true;
        Mouse.OverrideCursor = null;
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        // il est aussi possible de déclarer le délégué associé à DispatcherUnhandledException dans le code-behind
        // trap unhandled exceptions from the main UI dispatcher thread
        this.DispatcherUnhandledException += Application_DispatcherUnhandledException;

        // trap unhandled exceptions from all threads in the AppDomain
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

        // trap unhandled exceptions from within each AppDomain that uses a task scheduler for asynchronous operations
        TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
    }

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    { }

    void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
    {}

Application web API