Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Catching all Thrown Exceptions

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved [RESOLVED] [2.0] Catching all Thrown Exceptions

    Is there any kind of event or a way that would allow me to catch all exceptions that were not handled?

    Basically, my application will have the usual Try Catch blocks around code that can crash but what about code that shouldn't normally crash that doesn't have the blocks? I'd rather not put EVERYTHING in Try Catch blocks (that would be quite time consuming).

    Is there a way to basically catch an exception that isn't encapsulated in a Try Catch block?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Catching all Thrown Exceptions

    Wrap the contents of your startup method in a try.. catch block. Unhandled exceptions will jump back to that catch block and from there the best you can do is give a nice message and exit.

    e.g.
    Code:
    static void Main()
    {
      try {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
      }
      catch (Exception e) {
        // handle the exception
      }
    }

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0] Catching all Thrown Exceptions

    I didn't even think of that one.

    I've been doing some research and it looks like the only way to catch exceptions is in a catch block (common sense, right?). That's what I thought but I just wanted to double check to see if .Net 2.0 offered any additional functionality like an event that is initiated when an exception occurs or something.

    Thanks
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2.0] Catching all Thrown Exceptions

    There is an event that is fired upon the throwing of an unhandled exception. However, since, as you say, the only way to actually catch it is in a catch block, the event does not allow the exception to be handled - so it facilitates additional behaviour to the generic exception error only.

    Have a look here
    http://www.codinghorror.com/blog/archives/000216.html

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