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. C#
  4. How to trap unhandled exceptions

How to trap unhandled exceptions

Scheduled Pinned Locked Moved C#
csharpdotnethelptutorialquestion
7 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.
  • G Offline
    G Offline
    Glenn E Lanier II
    wrote on last edited by
    #1

    Hi. I'm trying to catch all unhandled exceptions in my application (so they can be logged and the user presented with a "nice" message instead of some confusing "computer-y" message. I do the following in my WinForm application:-- In the constructor // Create the unhandled exception handler AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(OverAllUnhandledHandler); -- the method static void OverAllUnhandledHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; logger.Log(LogLevel.Fatal, e.ToString()); MessageBox.Show("Please check error logs for details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
    If I create a divide by zero error, it is trapped (either in the constructor or in a button click event. I have even created a thread that starts and causes the error; it is still trapped. However, some errors are still popping up in the standard "Microsoft .NET Framework" dialog, with the text "An unhandled exception has occurred in your application.....". Any idea how to get all unhandled exceptions (I know, code better to catch all exceptions) to be caught by this OverAllUnhandledHandler method? Thanks for any tips. --G

    C D 2 Replies Last reply
    0
    • G Glenn E Lanier II

      Hi. I'm trying to catch all unhandled exceptions in my application (so they can be logged and the user presented with a "nice" message instead of some confusing "computer-y" message. I do the following in my WinForm application:-- In the constructor // Create the unhandled exception handler AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(OverAllUnhandledHandler); -- the method static void OverAllUnhandledHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; logger.Log(LogLevel.Fatal, e.ToString()); MessageBox.Show("Please check error logs for details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
      If I create a divide by zero error, it is trapped (either in the constructor or in a button click event. I have even created a thread that starts and causes the error; it is still trapped. However, some errors are still popping up in the standard "Microsoft .NET Framework" dialog, with the text "An unhandled exception has occurred in your application.....". Any idea how to get all unhandled exceptions (I know, code better to catch all exceptions) to be caught by this OverAllUnhandledHandler method? Thanks for any tips. --G

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I put my main code in a try/catch and handle threaded exceptions, it's a long time since my app blew up without logging a nice error for me and telling the user of the same.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      G 1 Reply Last reply
      0
      • C Christian Graus

        I put my main code in a try/catch and handle threaded exceptions, it's a long time since my app blew up without logging a nice error for me and telling the user of the same.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        G Offline
        G Offline
        Glenn E Lanier II
        wrote on last edited by
        #3

        Thanks -- haven't been handling threaded exceptions -- giving that a shot. Do you mean you put a try/catch around the Application.Run(formName); in Main()?

        C 1 Reply Last reply
        0
        • G Glenn E Lanier II

          Thanks -- haven't been handling threaded exceptions -- giving that a shot. Do you mean you put a try/catch around the Application.Run(formName); in Main()?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Yes, that's all I did initially, then as I am multithreaded, I had to add code to handle exceptions in threads.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          G 1 Reply Last reply
          0
          • G Glenn E Lanier II

            Hi. I'm trying to catch all unhandled exceptions in my application (so they can be logged and the user presented with a "nice" message instead of some confusing "computer-y" message. I do the following in my WinForm application:-- In the constructor // Create the unhandled exception handler AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(OverAllUnhandledHandler); -- the method static void OverAllUnhandledHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; logger.Log(LogLevel.Fatal, e.ToString()); MessageBox.Show("Please check error logs for details", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            If I create a divide by zero error, it is trapped (either in the constructor or in a button click event. I have even created a thread that starts and causes the error; it is still trapped. However, some errors are still popping up in the standard "Microsoft .NET Framework" dialog, with the text "An unhandled exception has occurred in your application.....". Any idea how to get all unhandled exceptions (I know, code better to catch all exceptions) to be caught by this OverAllUnhandledHandler method? Thanks for any tips. --G

            D Offline
            D Offline
            Daniel Grunwald
            wrote on last edited by
            #5

            I handle exceptions using: AppDomain.UnhandledException, System.Windows.Forms.Application.ThreadException, and a catch-all handler in void Main(). This gets all unhandled exceptions (well, except for some that are swallowed by Windows Forms/COM in the Drag* events - put catch handlers in all Drag* event handlers).

            G 1 Reply Last reply
            0
            • C Christian Graus

              Yes, that's all I did initially, then as I am multithreaded, I had to add code to handle exceptions in threads.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              G Offline
              G Offline
              Glenn E Lanier II
              wrote on last edited by
              #6

              Great. Thanks. I've included both the try/catch and the ThreadException code -- we'll see how it runs this afternoon!

              1 Reply Last reply
              0
              • D Daniel Grunwald

                I handle exceptions using: AppDomain.UnhandledException, System.Windows.Forms.Application.ThreadException, and a catch-all handler in void Main(). This gets all unhandled exceptions (well, except for some that are swallowed by Windows Forms/COM in the Drag* events - put catch handlers in all Drag* event handlers).

                G Offline
                G Offline
                Glenn E Lanier II
                wrote on last edited by
                #7

                Thanks Daniel -- this is now how the application works (I previously only had AppDomain.UnhandledException, thinking that it would get all unhandled exceptions). Since I don't (currently) do any drag events, I should be OK (but I'll keep it in mind for future projects). --G

                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