VCL Exceptions in C++ Builder 5

1.7k Views Asked by At

I'm trying to control exception raised in the code, however this sample is not working as it should under C++ Builder 5.

void __fastcall TForm1::Button1Click(TObject *Sender)
{ 
    try
    {
         throw Exception("NoNumber");
    }
    catch(Exception& E)
    {
        // but we never get the LALAL message
        ShowMessage("LALAL");
    }
}

Why is the catch block never reached when the exception is raised ?

1

There are 1 best solutions below

0
On BEST ANSWER

First, you should catch exceptions by const reference instead:

catch(const Exception& E)

That allows the compiler to emit slightly more efficient code for managing the exception. However, that alone would not prevent the exception from being caught.

If you are running the app inside of the debugger then keep in mind that the debugger will catch the exception first, so you have to tell the debugger to pass the exception back to your app for normal handling by pressing F9 or the Run toolbar button, or else configure the debugger to ignore exceptions.

If you are running the app outside of a debugger, then there is nothing wrong with the code you have shown that would prevent the catch from catching the exception under normal conditions.

I used BCB5 for years and this type of code has always worked just fine for me (though I always use const).