Click to See Complete Forum and Search --> : problem in exception handling
vinit005
June 18th, 2008, 12:02 AM
can anyone tell me how to provide user friendly message to the user when there is an exception..........suppose there is an exception while divide by zero......so what should i do to display the message to user as "divide by zero error"instead of showing system error..........
please someone help me..............
mwpeck
June 18th, 2008, 12:18 AM
You can try taking a look at the following:
http://www.dotnetjohn.com/articles.aspx?articleid=243
It shows you how to create your own custom exception (it even has an example of a custom exception for dividing by zero).
If someone has a better way of doing it the please post up how to do so.
Another way if you are only going to have one custom exception would be doing something like
try
{
Calculate();
}
catch (Exception)
{
MessageBox.Show("Error: Attempted to divide by zero.");
}
That WOULD work, but it would be better to learn the other method so your program is more flexible.
Shuja Ali
June 18th, 2008, 12:32 AM
can anyone tell me how to provide user friendly message to the user when there is an exception..........suppose there is an exception while divide by zero......so what should i do to display the message to user as "divide by zero error"instead of showing system error..........
please someone help me..............
You should be reading this.
http://msdn.microsoft.com/en-us/library/ms173160(VS.80).aspx
RaleTheBlade
June 19th, 2008, 12:04 AM
You can catch the precise exception and throw a friendlier exception, though be careful because when you throw a new exception, you lose the original stack trace which makes tracking it back difficult.
try
{
DoCalculations();
}
catch (DivideByZeroException)
{
throw new DivideByZeroException("Error: Attempted to divide by zero.");
}
I personally created a custom exceptions logger that logs exceptions to a simple plain text file at the time the exception is thrown and then throws a new friendlier exception.
try
{
DoCalculations();
}
catch (DivideByZeroException ex)
{
ExceptionManagement.Log(ex, ExceptionLevel.High);
throw new DivideByZeroException("Error: Attempted to divide by zero.");
}
The ExceptionManagement class is a class of my own design. But it REALLY helps when tracking bugs for fixes.
Mutant_Fruit
June 19th, 2008, 06:15 AM
You can catch the precise exception and throw a friendlier exception, though be careful because when you throw a new exception, you lose the original stack trace which makes tracking it back difficult.
Only if you do it wrong ;)
try
{
DoCalculations();
}
catch (DivideByZeroException ex)
{
throw new DivideByZeroException("Error: Attempted to divide by zero.", ex);
}
Just store the actual exception as the 'InnerException' of the new exception you created. I believe when you call .ToString() it will then be printed out.
RaleTheBlade
June 19th, 2008, 01:43 PM
Only if you do it wrong ;)
try
{
DoCalculations();
}
catch (DivideByZeroException ex)
{
throw new DivideByZeroException("Error: Attempted to divide by zero.", ex);
}
Just store the actual exception as the 'InnerException' of the new exception you created. I believe when you call .ToString() it will then be printed out.
Huh... learn something new everyday, lol. I didn't know you could do that. I think I have an unconscious tendency to stay away from InnerException because it caused me alot of problems when I was newer to C#. It was one of those traumatic, angering things that I couldn't figure out what it was for and whenever I tried to use it, it was almost always null so I just ignored it, hahaha.
zips
June 20th, 2008, 10:13 AM
Note that this code does NOT cause an exception to be thrown:Double A = 0D;
Double B = Math.PI / A;
It sets the value of B to Infinity, which then probably causes an exception when B is used in some other location.
You either have to check if A is zero before the divide or check if B is Infinity after the divide to impliment your user friendly exception.Double A = 0D;
Double B = Math.PI / A;
if (Double.IsInfinity(B))
{
throw new DivideByZeroException("Error: Attempted to divide by zero.");
}
TheCPUWizard
June 20th, 2008, 10:35 PM
Only if you do it wrong ;)
try
{
DoCalculations();
}
catch (DivideByZeroException ex)
{
throw new DivideByZeroException("Error: Attempted to divide by zero.", ex);
}
Just store the actual exception as the 'InnerException' of the new exception you created. I believe when you call .ToString() it will then be printed out.
True, however there are often security reasons why you WANT to lose that information.
For anyone seriously interested in exceptional exception handling ;) an excellent starting point is the Patterns and Practices Exception Handling Block (in conjunction with the Logging Block) that is part of Enterprise Library.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.