|
-
Mar 26th, 2006, 04:09 AM
#1
[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?
-
Mar 26th, 2006, 04:22 AM
#2
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
}
}
-
Mar 26th, 2006, 04:25 AM
#3
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
-
Mar 26th, 2006, 04:28 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|