Catching run time errors

M

Manish Soni

Hi,

A module which I am calling may have a run time error. Now currently in
such case user gets message box saying run time error has occured. My aim is
to catch this runtime error and show a friendly message to the user. How can
this be done? I have tried putting a try catch block around function call
but runtime error is not caught.

--Manish Soni
 
Z

Zach

You *have* to (!!!) establish where exactly in your program the error can
occur. Most runtime errors I have dealt with, have to do with conversions
between and to strings. Put MessageBox.Show("1"); 2,3,4 etc. all through
your problema area in your program and get it to produce an error. You will
then know where the error is occurring, then deal with that error. Allowing
the user to see bug messages is absolutely no good.

For instance:

try
{
some conversion giving x a value
}
catch
{
x = 0;
}
 
B

Bruce Wood

Yes and no.

First read the MSDN article. It points out quite clearly (and, IMHO,
correctly) that there are two kinds of exceptions in every program.
Exceptions that you can handle gracefully and continue executing, and
exceptions that you can't handle and have no choice but to shut down
the program.

In the former case, yes. You should figure out where the exception is
happening, trap it with a try...catch, and take remedial action.

In the latter case, you should allow the exception to bubble to the top
of the call stack and catch it in the AppDomain.UnhandledException and
the Application.ThreadException handlers. These handles should probably
log the details of the error in a log file somewhere, maybe send an
e-mail to someone in support, and present a pleasant message box to the
user saying nothing more than that something has gone horribly wrong,
the application must shut down now, and that they should call support.

On some occasions, this strategy is insufficient for dealing with fatal
exceptions, because you lose too much context by allowing the exception
to bubble to the top of the call stack, and the log messages are
missing important information. If this is the case, you should catch
the exception and wrap it in a new exception, adding the necessary
context information, and then throw that new exception and let it
bubble up into the global exception handlers.

Read the article: do not attempt to catch evey exception that can be
thrown by every call in your program. Catch only the ones that you can
deal with gracefully, and handle the rest globally.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top