urgent help needed with exception handling.

  • Thread starter Thread starter Jack jensen
  • Start date Start date
J

Jack jensen

Hello,
I have the code below :(simplified zip code format)


try
{

Convert.ToInt32(tbOShipPostalCode.Text);
}
catch
{
MessageBox.Show("The zip code must be a number.", "Zip code
error",
MessageBoxButtons.OK, MessageBoxIcon.Error);


}
The user should only see the messagebox when the input is not a number.

But they see message as if the exception was not beeing handled.
"
A first chance exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Input string was not in a correct format.
"

How can i avoid this message?

Many thanks in advance
JJ
 
Firstly - aren't first chance exceptions specific to the debugger?

Secondly - a better approach is to avoid the exception at all;

int value;
if(int.TryParse(text, out value)) {
// do something useful with value
} else {
MessageBox.Show("Whoops");
}

Marc
 
Back
Top