|
-
June 18th, 2008, 12:02 AM
#1
problem in exception handling
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..............
-
June 18th, 2008, 12:18 AM
#2
Re: problem in exception handling
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
Code:
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.
-
June 18th, 2008, 12:32 AM
#3
Re: problem in exception handling
 Originally Posted by vinit005
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/libr...60(VS.80).aspx
-
June 19th, 2008, 12:04 AM
#4
Re: problem in exception handling
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.
Code:
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.
Code:
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.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
June 19th, 2008, 06:15 AM
#5
Re: problem in exception handling
 Originally Posted by RaleTheBlade
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 
Code:
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
June 19th, 2008, 01:43 PM
#6
Re: problem in exception handling
 Originally Posted by Mutant_Fruit
Only if you do it wrong
Code:
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.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
June 20th, 2008, 10:13 AM
#7
Re: problem in exception handling
Note that this code does NOT cause an exception to be thrown:
Code:
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.
Code:
Double A = 0D;
Double B = Math.PI / A;
if (Double.IsInfinity(B))
{
throw new DivideByZeroException("Error: Attempted to divide by zero.");
}
-
June 20th, 2008, 10:35 PM
#8
Re: problem in exception handling
 Originally Posted by Mutant_Fruit
Only if you do it wrong
Code:
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|