Catch all errors

FuZion

Well-known member
Joined
Jan 28, 2007
Messages
47
Programming Experience
Beginner
Hello there,

I am writing a server application, and I was wondering if it is possible to catch all of the errors/exceptions thrown by the application, and write it to a log file? I've looked around (Google) a little bit but I haven't found anything that makes sense to me. I'm guessing its not as simple as adding Handels MyBase.error to the end of a Sub routine. Can anyone help me out?

Thanks in advance!

FuZion
 
HttpApplication.Error Event is for ASP.Net applications.

For Windows Forms you can use the application event UnhandledException, here you can set e.ExitApplication = False to let the application continue (even though the method that threw was broken). The application will still break when debugging through VS. You normally handle all exceptions imaginable, but this event will let you take on those not anticipated. I'm not sure if you meant also to log the handled exceptions, if so just add the log call in the Catch section of Try-Catch and such blocks.

For logging you can configure and use My.Application.Log or other logging methods.
 
I'm looking for both really. I was wondering if there was a way to do the "imaginary and imaginary" ones within one bock of code. I'd just record the line and error message, and I would be able to reference it later. So when you say this Try..Catch stuff, I've used the method before, and I'm wondering if you mean add this to the code under the UnhandledException event, or just a certain points in my code?

Thank you so much for you help!
 
You use Try-Catch and similar exception handling blocks to handle possible exceptions when you expect them at specific calls or blocks in code. "Unhandled" means that you don't catch the exception when it is thrown, that's when it goes to the UnhandledException event and finally thrown to user. Normally this means user is given a cryptic exception message with a choice to continue/end application, but as mentioned you can here intercept at last instance and let the application continue - but same as user at this stage you may leave it in an unpredictable state since something somewhere did went wrong in your code for it to happen.
 
Ok, that makes perfect sense. Now, I have a DLL that I reference in my project.. this DLL throws its own exceptions.. if I add the unhandeled event, will it catch those if I don't include any Try/Catches elsewhere in my code?
 
Yes, it's your application that runs the method that throws, when you don't handle it goes to unhandled..
 
Well I'm still having a slight problem. I am unable to add the event. VS is not allowing me to add My anywhere.. I'm able to add MyBase, but neither Application or UnhandeledException is in there. What am I do wrong?

Thank you!
 
Well I'm still having a slight problem. I am unable to add the event. VS is not allowing me to add My anywhere.. I'm able to add MyBase, but neither Application or UnhandeledException is in there. What am I do wrong?

Thank you!
Are you able to follow the directions given in the link I gave for the event? (1-3) then select the object and event in the 2 top combos.
 
Hm, I tried it again and I got it.. guess I just have to slow down a little. Thanks for your help!
 
Back
Top