Trapping Exception, yet system exception is still generated?

G

Guest

I have the following code and when the exception is raised the OS displays a nasty Stop dialog box
it says an unhandled exception occured, gives 3 buttons to click details, contintue Quit... i do not want this ugly thing to display, what am i missing from the code below? am i not catching that exception and displaying my own error message? so why is the other one coming up after mine

public bool isInteger(string str

int i=0
bool res=false
if (str != null)

try

i = Int32.Parse(str)
res = true
}
catch (FormatException e)

MessageBox.Show("Make sure to input only numeric values");

}
return res
}
 
T

Trevor

CSharpX said:
I have the following code and when the exception is raised the OS displays a nasty Stop dialog box
it says an unhandled exception occured, gives 3 buttons to click details,
contintue Quit... i do not want this ugly thing to display, what am i
missing from the code below? am i not catching that exception and displaying
my own error message? so why is the other one coming up after mine?
public bool isInteger(string str)
{
int i=0;
bool res=false;
if (str != null)
{
try
{
i = Int32.Parse(str);
res = true;
}
catch (FormatException e)
{
MessageBox.Show("Make sure to input only numeric values");
}
}
return res;
}

MSDN says this method can throw a FormatException, ArgumentNullException,
ArgumentException, FormatException, OverflowException. You only handle
FormatException. To fix your problem catch ANY exception that may be
thrown. As you have found out, one cannot assume that the only exception
that will ever be thrown by this method is a FormatException.
 
C

Christopher Kimbell

The code only catches FormatException, any other will be forwarded up the
call stack.
OverflowException is another exception that can be raised by the Parse
method, is this the one you are getting?

Chris

CSharpX said:
I have the following code and when the exception is raised the OS displays a nasty Stop dialog box
it says an unhandled exception occured, gives 3 buttons to click details,
contintue Quit... i do not want this ugly thing to display, what am i
missing from the code below? am i not catching that exception and displaying
my own error message? so why is the other one coming up after mine?
 
G

Guest

Ahhh ok, i get it now, we catch all the exceptions instead of just one
Ok thanks for the help
 

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