473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Catching run time errors

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
Nov 17 '05 #1
3 1797
Look at the documentation for the AppDomain.UnhandledException event
and the Application.ThreadException event. Also, look at this article
in MSDN:

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/

Nov 17 '05 #2
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;
}
Nov 17 '05 #3
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.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Manish Soni | last post by:
My C# code invokes some unamanged code. My goal is to somehow catch any time error in the unmanaged code. I want to capture that a show a user friendly exception to the end user. -- Manish
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
13
by: Chris Stankevitz | last post by:
Hi, I have a very large Visual c++ .net 2003 7.1 native c application (approximately 500,000 lines of code). This application is a simulation that frequently works with floating point numbers....
0
by: R. MacDonald | last post by:
Hi, I have a VB.Net application that dynamically calls unmanaged routines in a DLL that has been created by Fortran (DVF v5). The call to the DLL routine is within a Try/Catch block, but when...
6
by: rodchar | last post by:
hey all, is there a way to catch all errors for my webform without using the global.asax page? thanks, rodchar
4
by: John Pye | last post by:
Hi all I have some C code that is giving me some 'nan' values in some calculations. The C code is wrapped using SWIG to give me a Python module that I am then exercising through a unittest...
25
by: JJ | last post by:
I only want to catch 404 errors at the application level (the rest are will be handled by the customerrors section of the web.config). How do I check for the error code in the Application_Error...
14
by: Donn Ingle | last post by:
Yo, An app of mine relies on PIL. When PIL hits a certain problem font (for unknown reasons as of now) it tends to segfault and no amount of try/except will keep my wxPython app alive. My first...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.