Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching all exceptions at one place

Status
Not open for further replies.

Ator

Programmer
Jan 23, 2006
26
SE
Hi

Is it possible to catch all exceptions in _one_ place?
I want to have a error-report system, that catches _all_ exceptions (including the exceptions that have been specially handled).

I´ve only come up with:
[STAThread]
static void Main() {
try {
Application.Run(new Main());
} catch(Exception ex) {
// Write error to database or something.
}
}

But this terminates the program, which I dont want to.

Anyone know if there exists a "built-in" error-report handling system or something similiar?

Im using VS2003.

Thanks
 
As far as I know, I don't think you can do what you want to do.

You can create one error handler and call the same one, passing it the exception from your catch statements all over your app, but I don't think you can declare an error handler one time and have it automatically catch all exceptions all over the application, across multiple classes.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
A Utopian Idea.

You're basically asking for a "bug-free" program. Depending on how your code is architected, you could implement a few patterns that would allow you to catch a lot of bugs in one place.

If you are looking to have an app that just - does not quit when an exception is thrown, then I recommend you implement some kind of controller. Make a class (possibly a singleton if only one instance of the application will ever run on a box) and make all your data pass through it. Have it CONTROL your data. That way, when a form (a.k.a a view) dies, your app can catch the termination of that thread and relaunch a new thread of the same view. You can then re-populate all of its data from the controller. Take a look at the Model-View-Controller design pattern.

there really is no way to stop a program from ever crashing though - welcome to computers!
 
I dont mind if the program some times crashes.
But if I can store all exceptions, or the most of them, Im happy.

If I give away a program of mine to somebody, I want to store these errors as feedback for me. Then, when updating the program, I see the errors and I hopefully can fix them more or less.

I think that putting a "try...catch" in everyplace that something can go wrong (which is ALOT), the code till be messy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top