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. Handler for all unhandled exceptions in a class?

Handler for all unhandled exceptions in a class?

Scheduled Pinned Locked Moved C#
questionhelptutorial
7 Posts 5 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.
  • A Offline
    A Offline
    alanteigne
    wrote on last edited by
    #1

    Hello, this may be a stupid question but I'm curious. I have a class I've created (that extends the FORM class) that runs as a child form of my main application. The purpose and nature of the form is such that it doesn't matter if it closes...the parent form monitors and will reopen it if necessary. I am handling several types of exceptions and closing the child form when they occur. However, I occasionally get an unhandled exception (in the child form) that causes it to crash. I'm looking for a way to catch all unhandled exceptions in my custom class and run an event accordingly. Is there a way to do this? I see a method for any unhandled exceptions for the whole app, but I don't want that. I also see how to handle all exceptions when my child form is initialized, but that doesn't help if it's already initialized and running. All I want now is to handle all exceptions in my custom class. Is this possible? Thanks, Alan

    E M M G A 5 Replies Last reply
    0
    • A alanteigne

      Hello, this may be a stupid question but I'm curious. I have a class I've created (that extends the FORM class) that runs as a child form of my main application. The purpose and nature of the form is such that it doesn't matter if it closes...the parent form monitors and will reopen it if necessary. I am handling several types of exceptions and closing the child form when they occur. However, I occasionally get an unhandled exception (in the child form) that causes it to crash. I'm looking for a way to catch all unhandled exceptions in my custom class and run an event accordingly. Is there a way to do this? I see a method for any unhandled exceptions for the whole app, but I don't want that. I also see how to handle all exceptions when my child form is initialized, but that doesn't help if it's already initialized and running. All I want now is to handle all exceptions in my custom class. Is this possible? Thanks, Alan

      E Offline
      E Offline
      eggsovereasy
      wrote on last edited by
      #2

      You can put a try block where your form gets started. try {   Application.Run(new Form1()); } catch (Exception ex) {   LogException(ex);   Application.Exit(); }

      A 1 Reply Last reply
      0
      • E eggsovereasy

        You can put a try block where your form gets started. try {   Application.Run(new Form1()); } catch (Exception ex) {   LogException(ex);   Application.Exit(); }

        A Offline
        A Offline
        alanteigne
        wrote on last edited by
        #3

        eggs, thanks for the suggestion! i believe that code will catch any unhandled exceptions generated by my entire app (parent form, and my custom child form). i'm trying to just catch errors in the child form. the child form is being added something like this: frmCustomForm = new MyCustomForm(); this.Controls.Add(frmCustomForm); i would like any unhandled exception that occurs within that custom form to cause it to trigger an event. alan

        1 Reply Last reply
        0
        • A alanteigne

          Hello, this may be a stupid question but I'm curious. I have a class I've created (that extends the FORM class) that runs as a child form of my main application. The purpose and nature of the form is such that it doesn't matter if it closes...the parent form monitors and will reopen it if necessary. I am handling several types of exceptions and closing the child form when they occur. However, I occasionally get an unhandled exception (in the child form) that causes it to crash. I'm looking for a way to catch all unhandled exceptions in my custom class and run an event accordingly. Is there a way to do this? I see a method for any unhandled exceptions for the whole app, but I don't want that. I also see how to handle all exceptions when my child form is initialized, but that doesn't help if it's already initialized and running. All I want now is to handle all exceptions in my custom class. Is this possible? Thanks, Alan

          M Offline
          M Offline
          martin_hughes
          wrote on last edited by
          #4

          Here's an idea of what you could do (although I make no assurances as to the quality of the the design! :) ) - In your custom form you'll need to surround any potentially error producing blocks of code in a standard try...catch block. What you'll also need in the custom class is an event, perhaps call it OnError or similar (have a google for how events are implemented in C#), and when an error is caught by the try...catch mechanism, fire off the OnError event. Then from your main app you can subscribe to this event with something like the following: {... frmCustomForm = new MyCustomForm(); frmCustomForm.OnError += new EventHandler(yourMethodHere); this.Controls.Add(frmCustomForm); ...} private void yourMethodHere() { code that runs when the event is fired an recieved. } Now a word of warning :) It sounds as if there's a lot of logic contained in your custom form that might suit being refactored out... something for you to have a think about!

          "On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't. "I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it." -Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.

          1 Reply Last reply
          0
          • A alanteigne

            Hello, this may be a stupid question but I'm curious. I have a class I've created (that extends the FORM class) that runs as a child form of my main application. The purpose and nature of the form is such that it doesn't matter if it closes...the parent form monitors and will reopen it if necessary. I am handling several types of exceptions and closing the child form when they occur. However, I occasionally get an unhandled exception (in the child form) that causes it to crash. I'm looking for a way to catch all unhandled exceptions in my custom class and run an event accordingly. Is there a way to do this? I see a method for any unhandled exceptions for the whole app, but I don't want that. I also see how to handle all exceptions when my child form is initialized, but that doesn't help if it's already initialized and running. All I want now is to handle all exceptions in my custom class. Is this possible? Thanks, Alan

            M Offline
            M Offline
            Mark Churchill
            wrote on last edited by
            #5

            You could try looking at this: http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx[^]

            Mark Churchill Director Dunn & Churchill Diamond Binding: Zero to Data Layer in 3 mins

            1 Reply Last reply
            0
            • A alanteigne

              Hello, this may be a stupid question but I'm curious. I have a class I've created (that extends the FORM class) that runs as a child form of my main application. The purpose and nature of the form is such that it doesn't matter if it closes...the parent form monitors and will reopen it if necessary. I am handling several types of exceptions and closing the child form when they occur. However, I occasionally get an unhandled exception (in the child form) that causes it to crash. I'm looking for a way to catch all unhandled exceptions in my custom class and run an event accordingly. Is there a way to do this? I see a method for any unhandled exceptions for the whole app, but I don't want that. I also see how to handle all exceptions when my child form is initialized, but that doesn't help if it's already initialized and running. All I want now is to handle all exceptions in my custom class. Is this possible? Thanks, Alan

              G Offline
              G Offline
              Giorgi Dalakishvili
              wrote on last edited by
              #6

              What about Application..::.ThreadException Event[^]

              #region signature my articles #endregion

              1 Reply Last reply
              0
              • A alanteigne

                Hello, this may be a stupid question but I'm curious. I have a class I've created (that extends the FORM class) that runs as a child form of my main application. The purpose and nature of the form is such that it doesn't matter if it closes...the parent form monitors and will reopen it if necessary. I am handling several types of exceptions and closing the child form when they occur. However, I occasionally get an unhandled exception (in the child form) that causes it to crash. I'm looking for a way to catch all unhandled exceptions in my custom class and run an event accordingly. Is there a way to do this? I see a method for any unhandled exceptions for the whole app, but I don't want that. I also see how to handle all exceptions when my child form is initialized, but that doesn't help if it's already initialized and running. All I want now is to handle all exceptions in my custom class. Is this possible? Thanks, Alan

                A Offline
                A Offline
                alanteigne
                wrote on last edited by
                #7

                Thanks for all of the suggestions. I ended up using pretty much all of them in some form or another. I've isolated this custom class to run as a Windows service, and handle all UI and non UI unhandled exceptions with global handlers under Main(). Alan

                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