Results 1 to 6 of 6

Thread: Global exception handler

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Global exception handler

    Hi,

    Would a global exception handler be considered lazy?
    I mean I can use Try-Catch statements everywhere, but doesn't a global handler do the same?

    Also, can I re-throw an exception from a catch block?

    Thanks.
    Delete it. They just clutter threads anyway.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Global exception handler

    A global exception handler is always a good idea, if not a downright necessity. You should always include explicit exception handling, i.e. try...catch blocks, for any exceptions that you are aware of that could be thrown. That would include anywhere you're perfroming data access, disk I/O or anything else where there's an element beyond your control.

    We all make mistakes though, and there may be points in your code that you have missed a possible exception or even introduced one. If you don't have a global exception handler then, at those points, your app will crash. That's just plain bad. A global exception handler won't fix the problem but it will allow you to exit gracefully while informing the user and logging the error.

    Yes, you can re-throw an exception from a 'catch' block. In the C# IDE, if you type try and then hit Tab, this is what you get:
    csharp Code:
    1. try
    2. {
    3.  
    4. }
    5. catch (Exception)
    6. {
    7.    
    8.     throw;
    9. }
    Note that the exception that was caught is implicitly re-thrown by the 'throw' statement. You could do this too:
    csharp Code:
    1. try
    2. {
    3.  
    4. }
    5. catch (Exception ex)
    6. {
    7.    
    8.     throw ex;
    9. }
    but that's generally bad. By explicitly throwing the exception you remove its original call stack and the point it was re-thrown appears to be the source. You might also do this:
    csharp Code:
    1. try
    2. {
    3.  
    4. }
    5. catch (Exception ex)
    6. {
    7.    
    8.     throw new Exception("This is a custom exception", ex);
    9. }
    This allows you to wrap an exception thrown by the system into a custom exception of your own creation. You'll see various exceptions that you catch yourself with inner exceptions and that's how they are created: catch, wrap, throw.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Global exception handler

    That sounds reasonable.
    But is it okay to let (at least custom) fatal exceptions stay unhandled?
    Or would that be bad practice?

    Also, I have a handler on Application.ThreadException and a handler on AppDomain.UnhandledException.
    When is one fired and when is the other one?

    Thanks.

    Quote Originally Posted by jmcilhinney View Post
    In the C# IDE, if you type try and then hit Tab, this is what you get: ...
    Nice, didn't know that one
    Delete it. They just clutter threads anyway.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Global exception handler

    Quote Originally Posted by TheBigB View Post
    That sounds reasonable.
    But is it okay to let (at least custom) fatal exceptions stay unhandled?
    Or would that be bad practice?
    It' reasonable for a DLL to throw exceptions and assume that they will be handled by the referencing application but allowing exceptions to go unhandled in an application is terrible. Under what circumstances would allowing your app to crash with no logging and no explanation to the user be acceptable?
    Quote Originally Posted by TheBigB View Post
    Also, I have a handler on Application.ThreadException and a handler on AppDomain.UnhandledException.
    When is one fired and when is the other one?
    I had no idea myself so I just opened the documentation for ThreadException. Thirty seconds work and I had the answer. If I can do that then you can. ALWAYS read the documentation first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Global exception handler

    Quote Originally Posted by jmcilhinney View Post
    It' reasonable for a DLL to throw exceptions and assume that they will be handled by the referencing application but allowing exceptions to go unhandled in an application is terrible. Under what circumstances would allowing your app to crash with no logging and no explanation to the user be acceptable?
    But what if I make the global handlers do that? Is that a bad idea?


    Quote Originally Posted by jmcilhinney View Post
    I had no idea myself so I just opened the documentation for ThreadException. Thirty seconds work and I had the answer. If I can do that then you can. ALWAYS read the documentation first.
    Sorry found it.
    Delete it. They just clutter threads anyway.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Global exception handler

    A global exception handler is something you would add to an application, not a library. Libraries don't crash because they don't run on their own. A library is a producer. An application is a consumer.

    It's really very simple:

    1. Anywhere that it makes sense to throw an exception, you should throw an exception.
    2. Any code you write that can reasonably throw an exception should be wrapped in an exception handler UNLESS it is reasonable to assume that the exception will be handled at a higher level.
    3. Always add a global exception handler to an application so that your application can exit gracefully with notification if an unexpected error occurs instead of falling in a heap and making you look like a bad programmer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width