Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception is not caught although it is included in catch statement

I have this program written in C++ Builder 6. I didn't write all the code, just some of it. The language, however, is not C++ (as far as I'm aware) - it looks more like Delphi or Pascal. So that's why I included them all in the tags.

I have an int called Oversteering.

try
{
    Oversteering=HoursCounter.ToInt();
}
catch(EConvertError &convertError)
{
    Oversteering=0;
}

HoursCounter is an AnsiString, and it is in the form of an int.

Since this is the only try/catch statement in the whole code (that's not too good, I know), and I couldn't find any good example of such in Delphi/Pascal/???, I don't know if it's correctly written.

Well, I try to convert the string to an int. Sometimes I get this error:

error

That is, an exception called EConvertError has occurred.

So my question is: why is this exception NOT caught by the catch statement?

like image 707
gosr Avatar asked Nov 25 '25 11:11

gosr


2 Answers

This error is shown by the debugger when running through the code, if you run the exe and have the same situation the error message will not be shown to you

The exception is caught but the debugger is notifiying you regarding the error in the code

that is here

  try
     {
      Oversteering=HoursCounter.ToInt();
     }

since running in the debugger the ,your trying to convert (blankspace) '' to integer, the debugger will show the exception...but when running the exe, the debugger will set

       Oversteering=0

check this from about.com

Break On Exceptions When building a program with exception handling, you may not want Delphi to break on Exceptions. This is a great feature if you want Delphi to show where an exception has occurred; however, it can be annoying when you test your own exception handling.

like image 161
PresleyDias Avatar answered Nov 28 '25 02:11

PresleyDias


As @PresleyDias explained, it is the debugger that is displaying the exception, not your app. The exception is being caught (you should be catching it by a const reference, though), but the debugger sees it before your app does, that's all. You can configure the debugger to ignore EConvertError, if you like.

A better solution is to avoid the exception in the first place. If you use AnsiString::ToIntDef() instead, you can remove the try/catch block completely:

Oversteering = HoursCounter.ToIntDef(0); 

Alternatively, you can use TryStrToInt() instead:

if (!TryStrToInt(HoursCounter, Oversteering))
{
    ...;
}

If 0 is a valid value for your counter, use TryStrToInt():

if (TryStrToInt(HoursCounter, Oversteering))
{
    // use Oversteering as needed, even zeros...
}
else
    ShowMessage("Cannot convert HoursCounter to a valid integer!");

If 0 always represents an error, then use ToIntDef():

Oversteering = HoursCounter.ToIntDef(0);
if (Oversteering != 0)
{
    // use Oversteering as needed, except zeros...
}
else
    ShowMessage("Cannot convert HoursCounter to an acceptable integer!");
like image 25
Remy Lebeau Avatar answered Nov 28 '25 01:11

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!