Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception problem 1

Status
Not open for further replies.

ZoneVM

Programmer
Apr 7, 2005
30
US
I have the following method in my code:

bool TDefineEventForm::isValidTime(String time)
{
try
{
if (time.Length() < 7)
{
return false;
}
time = time.Trim();
int colPos = time.AnsiPos(":");
String hr = time.SubString(0,colPos-1);
String min = time.SubString(colPos+1,2);
int len = time.Length();
String ampm = time.SubString(len-1,2);
if ((ampm.LowerCase()!="am")&&(ampm.LowerCase()!"pm"))
{
return false;
}
hr.ToInt();
min.ToInt();
}
catch (const EConvertError &E)
{
return false;
}
catch (...)
{
return false;
}

return true;
}


The problem is that neither one of my catch blocks will catch the exception if the ToInt() methods throw an exception. It always terminates my application, and tells me that a EConvertError was raised by ToInt(). Can anyone see a problem with what I'm doing? Using C++ builder 6.

Thanks!
 
Just to debug this, let's change things around a bit. Also, I notice a typo.
Code:
if (time.Length() < 7)
{
    return false;
}
time = time.Trim();
int colPos = time.AnsiPos(":");
String hr = time.SubString(0,colPos-1);
String min = time.SubString(colPos+1,2);
int len = time.Length();
String ampm = time.SubString(len-1,2);
if ((ampm.LowerCase()!="am")&&(ampm.LowerCase() != "pm")) // typo
{
    return false;
}
try
{
    hr.ToInt();
    min.ToInt();
}
/* Lets remove this for testing, too
   catch (const EConvertError &E)
   {
      return false;
   }
*/
catch (...)
{
    String Opps = "I caught an exception. Hour = " + hr;
    Opps += " and Min = " + min;
    Application->MessageBox(Opps.c_str(), "Exception Caught", MB_OK);
    return false;
}

return true;

One more thing to note: if you are in debug mode, the exception will be caught by the debugger before your catch statement executes. If you resume the program, your catch statement will then run.

James P. Cottingham
-----------------------------------------
[sup]To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.[/sup]
 
Thanks! debug mode - that's what I was missing! appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top