Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Global Exception Handling

Global Exception Handling

Scheduled Pinned Locked Moved WPF
wpfhelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have to following in my App.xaml code behind:

    public partial class App : Application
    {
    public App()
    {
    }

    private void Application\_Startup(object sender, StartupEventArgs e)
    {
        Application.Current.DispatcherUnhandledException += dispatchUnhandled;
        AppDomain.CurrentDomain.UnhandledException += applicationUnhandledException;
    }
    
    private void dispatchUnhandled(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        var errorMessage = getInnerException(e.Exception);
    
        displayErrorMessage(errorMessage, e.Exception);
        e.Handled = true;
    }
    
    private static void applicationUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var ex = e.ExceptionObject as Exception;
        var errorMessage = getInnerException(ex);
    
        displayErrorMessage(errorMessage, ex);
    }
    
    private static void displayErrorMessage(string errorMessage, Exception ex)
    {
        logException(errorMessage, ex);
    
        ErrorHandlerViewModel vm = new ErrorHandlerViewModel();
        vm.ErrorMessage = errorMessage;
        vm.ErrorText = ex.ToString();
    
        ErrorHandlerView view = new ErrorHandlerView();
        view.DataContext = vm;
        view.ShowDialog();
    
        Application.Current.Shutdown();
    }
    
    private static string getInnerException(Exception e)
    {
        string msg = e.InnerException == null ? e.Message : getInnerException(e.InnerException);
        return msg;
    }
    
    private static void logException(string errorMessage, Exception ex)
    {
        Logger.LogMessage(errorMessage);
        Logger.LogMessage(ex.ToString());
    }
    

    }

    Application_Startup DOES run, but no exceptions are caught. What's wrong here? Thanks

    If it's not broken, fix it until it is

    Richard DeemingR M 2 Replies Last reply
    0
    • K Kevin Marois

      I have to following in my App.xaml code behind:

      public partial class App : Application
      {
      public App()
      {
      }

      private void Application\_Startup(object sender, StartupEventArgs e)
      {
          Application.Current.DispatcherUnhandledException += dispatchUnhandled;
          AppDomain.CurrentDomain.UnhandledException += applicationUnhandledException;
      }
      
      private void dispatchUnhandled(object sender, DispatcherUnhandledExceptionEventArgs e)
      {
          var errorMessage = getInnerException(e.Exception);
      
          displayErrorMessage(errorMessage, e.Exception);
          e.Handled = true;
      }
      
      private static void applicationUnhandledException(object sender, UnhandledExceptionEventArgs e)
      {
          var ex = e.ExceptionObject as Exception;
          var errorMessage = getInnerException(ex);
      
          displayErrorMessage(errorMessage, ex);
      }
      
      private static void displayErrorMessage(string errorMessage, Exception ex)
      {
          logException(errorMessage, ex);
      
          ErrorHandlerViewModel vm = new ErrorHandlerViewModel();
          vm.ErrorMessage = errorMessage;
          vm.ErrorText = ex.ToString();
      
          ErrorHandlerView view = new ErrorHandlerView();
          view.DataContext = vm;
          view.ShowDialog();
      
          Application.Current.Shutdown();
      }
      
      private static string getInnerException(Exception e)
      {
          string msg = e.InnerException == null ? e.Message : getInnerException(e.InnerException);
          return msg;
      }
      
      private static void logException(string errorMessage, Exception ex)
      {
          Logger.LogMessage(errorMessage);
          Logger.LogMessage(ex.ToString());
      }
      

      }

      Application_Startup DOES run, but no exceptions are caught. What's wrong here? Thanks

      If it's not broken, fix it until it is

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Are any exceptions thrown? If so, where are they thrown from?


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Are any exceptions thrown? If so, where are they thrown from?


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        yes, I added this in the MainWindowViewModel:

        int x = 0;
        int y = 10;
        int z = y / x;

        It throws, it doesn't get caught in the global exception handler.

        If it's not broken, fix it until it is

        Richard DeemingR 1 Reply Last reply
        0
        • K Kevin Marois

          yes, I added this in the MainWindowViewModel:

          int x = 0;
          int y = 10;
          int z = y / x;

          It throws, it doesn't get caught in the global exception handler.

          If it's not broken, fix it until it is

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          What triggers that code to run?


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          K 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            What triggers that code to run?


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            It's the main window in a WPF app. It's called from App.xaml

            If it's not broken, fix it until it is

            1 Reply Last reply
            0
            • K Kevin Marois

              I have to following in my App.xaml code behind:

              public partial class App : Application
              {
              public App()
              {
              }

              private void Application\_Startup(object sender, StartupEventArgs e)
              {
                  Application.Current.DispatcherUnhandledException += dispatchUnhandled;
                  AppDomain.CurrentDomain.UnhandledException += applicationUnhandledException;
              }
              
              private void dispatchUnhandled(object sender, DispatcherUnhandledExceptionEventArgs e)
              {
                  var errorMessage = getInnerException(e.Exception);
              
                  displayErrorMessage(errorMessage, e.Exception);
                  e.Handled = true;
              }
              
              private static void applicationUnhandledException(object sender, UnhandledExceptionEventArgs e)
              {
                  var ex = e.ExceptionObject as Exception;
                  var errorMessage = getInnerException(ex);
              
                  displayErrorMessage(errorMessage, ex);
              }
              
              private static void displayErrorMessage(string errorMessage, Exception ex)
              {
                  logException(errorMessage, ex);
              
                  ErrorHandlerViewModel vm = new ErrorHandlerViewModel();
                  vm.ErrorMessage = errorMessage;
                  vm.ErrorText = ex.ToString();
              
                  ErrorHandlerView view = new ErrorHandlerView();
                  view.DataContext = vm;
                  view.ShowDialog();
              
                  Application.Current.Shutdown();
              }
              
              private static string getInnerException(Exception e)
              {
                  string msg = e.InnerException == null ? e.Message : getInnerException(e.InnerException);
                  return msg;
              }
              
              private static void logException(string errorMessage, Exception ex)
              {
                  Logger.LogMessage(errorMessage);
                  Logger.LogMessage(ex.ToString());
              }
              

              }

              Application_Startup DOES run, but no exceptions are caught. What's wrong here? Thanks

              If it's not broken, fix it until it is

              M Offline
              M Offline
              maddymaddy14
              wrote on last edited by
              #6

              When an unhandled exception is thrown Application.Current.DispatcherUnhandledException is invoked. Hence the code written in your handler 'dispatchUnhandled' is executed. Do i miss your specific question?

              Manish Jain (MJ)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups