Question

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");
}
Was it helpful?

Solution

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");
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top