0
votes

Pourquoi app.dispatcherunhandledexception ManuLer n'atteignait pas les exceptions projetées par App Constructor?

Veuillez regarder le code suivant:

public partial class App : Application
{
        public App():base()
        {
                this.DispatcherUnhandledException += App_DispatcherUnhandledException;
                throw new InvalidOperationException("exception");
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
                MessageBox.Show(e.Exception.Message);
                e.Handled = true;
        }
}


3 Réponses :


0
votes

J'ai fonctionné de cette façon de faire la même chose.

public partial class App : Application
{
        public App():base()
        {
                Application.Current.DispatcherUnhandledException += new
                   System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(
                      AppDispatcherUnhandledException);
                throw new InvalidOperationException("exception");
        }

void AppDispatcherUnhandledException(object sender,
           System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //do whatever you need to do with the exception
            //e.Exception
              MessageBox.Show(e.Exception.Message);
                e.Handled = true;

        }
}


0 commentaires

0
votes

L'instance de l'application n'a pas été construite, donc l'application.current n'a aucun sens. Vous devez vous abonner sur AppDomain.CurrentDomaine.UnhandledException

public App() : base()
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    throw new InvalidOperationException("exception");
}

private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.ExceptionObject.ToString());
}


0 commentaires

0
votes

Pourquoi le gestionnaire ne comprend pas l'exception projetée par app constructeur?

Tout simplement parce qu'il n'y a pas de répartiteur exécuté avant que l'application a été construite.

Il s'agit de la méthode principale générée par le Compilateur: xxx

à partir du DOCS :

Lorsque exécuter est appelé, application attache un nouvel dispatcher instance sur le fil de l'interface utilisateur. Ensuite, la méthode de l'objet est appelée, qui démarre une pompe à message pour traiter les messages Windows.

Vous ne pouvez pas appeler exécuter avant que vous ayez créé l'objet objet.


0 commentaires