views:

40

answers:

2

I'm trying to catch a 'specific' exception (FormatException^ or OverflowException^) and then re throw it and catch it in the 'general' exception (Exception^) catch block.

When run, I give it a format exception through input. I then get this error in a dialog box: "An unhandled exception of type 'System.FormatException' occurred in FutureValue.exe Additional information: Input string was not in a correct format." When I click 'break' it takes me to line # 232.

Here is the partial code:

try
{
...
}
catch(FormatException^ ex)
{
      MessageBox::Show("FormatException Occured.  Message: " + ex->Message);
      throw;
}
***line# 232*** catch(OverflowException^ ex)
{
      MessageBox::Show("Overflow Occured. Message: " + ex->Message);
      throw;
}
catch(Exception^ ex)
{
      MessageBox::Show("TESTING");
}
A: 

is there a try catch block above this?

You just threw an exception with the throw statement, nobody is catching it. The debugger has taken you to where the exception was thrown

pm100
Oh, thanks. Duh. For some reason I was thinking that it would continue after it's catch block and the catch blocks after it would have a chance to catch the re-thrown exception.
Gijera
I see now, that a re-thrown exception, continues after the try block itself, to any subsequent try-catch blocks.
Gijera
actually I am totally wrong. You retrhow should have been caught by the outer try / catch (Exception) block
pm100
+1  A: 

The rethrow expression (throw without assignment_expression) causes the originally thrown object to be rethrown. Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next dynamically enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred.

Taking above into account, you may want to write your code like this:

try
{
    try
    {
       //...
    }
    catch(FormatException^ ex)
    {
          MessageBox::Show("FormatException Occured.  Message: " + ex >Message);
          throw;
    }
    catch(OverflowException^ ex)
    {
          MessageBox::Show("Overflow Occured. Message: " + ex->Message);
          throw;
    }
}
catch(Exception^ ex)
{
      MessageBox::Show("TESTING");
}
Vlad Lazarenko
Oh, thanks. Duh. For some reason I was thinking that it would continue after it's catch block and the catch blocks after it would have a chance to catch the re-thrown exception. I see now, that a re-thrown exception, continues after the try block itself, to any subsequent try-catch blocks. –
Gijera