Recent

Author Topic: basics of exception handling  (Read 5809 times)

erkka

  • New Member
  • *
  • Posts: 37
basics of exception handling
« on: September 16, 2010, 11:53:53 am »
Well, I'm writing a piece of code which tries to open a TDbf-database. If it fails to open, it will create the table. Just to test, I made this:

Code: [Select]
  try
    DbfOstot.Open;
  except
    on E: Exception do ShowMessage (E.Message);
  end;

When I comple & run, it halts at DbfOstot.Open, complaining that "project Test raised exception class 'Exception'.

What am I missing? I thought that try ... except is to catch exceptions. But now it pauses program execution at that point. If I hit F9 to continue execution, nothing happens.

What am I missing?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: basics of exception handling
« Reply #1 on: September 16, 2010, 12:04:17 pm »
Yes, but AFAIK if you run it from the IDE (that is, it runs through debugger) the debugger will catch that first. Try running outside the IDE (or simply turn off the debugger).

motaz

  • Sr. Member
  • ****
  • Posts: 495
    • http://code.sd
Re: basics of exception handling
« Reply #2 on: September 16, 2010, 12:08:01 pm »
when you press F9 you should get your exception message (ShowMessage..)

bflm

  • Jr. Member
  • **
  • Posts: 54
    • Free Pascal Random Bits
Re: basics of exception handling
« Reply #3 on: September 16, 2010, 12:09:23 pm »
When I comple & run, it halts at DbfOstot.Open, complaining that "project Test raised exception class 'Exception'.

As above posters said, the debugger intercepts the exception (pretty useful in most cases). It should be possible to disable that (for specific exception classes) in Environment | Options | Debugger | Language Exceptions, but I haven't tried/verified that.

erkka

  • New Member
  • *
  • Posts: 37
Re: basics of exception handling
« Reply #4 on: September 16, 2010, 12:28:18 pm »
Thank you everybody!

I turned off the debugger and now it works fine.

Still, I don't know why it did hang when the debugger was on and I hit F9. Might be a bug somewhere?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: basics of exception handling
« Reply #5 on: September 16, 2010, 12:34:36 pm »
Quote
Still, I don't know why it did hang when the debugger was on and I hit F9. Might be a bug somewhere?
No, it doesn't. The debugger catches the exception, and wait for your command. If you let it continue then your exception handling code will be executed.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1945
Re: basics of exception handling
« Reply #6 on: September 16, 2010, 12:38:43 pm »
Imho it's not a good idea to (ab-)use try-execpt for foreseeable problems.
Better use
if FileExists(FileName) then
or something.

You can still use try-except, if for example the file is corrupted, but not if the file does not exist.

erkka

  • New Member
  • *
  • Posts: 37
Re: basics of exception handling
« Reply #7 on: September 16, 2010, 12:48:17 pm »
Theo: You have a point there!

Leledumbo: That is how I thought, and I did some testing with debugger on.

Code: [Select]
 try
    DbfOstot.Open;
  except
    on E: Exception do CreateOstot;  

works fine. It pauses at DbfOstot.Open, but when I hit F9 it resumes and goes to CreateOstot to create the database.

But

Code: [Select]
try
    DbfOstot.Open;
  except
    on E: Exception do ShowMessage (E.Message);

pauses at DbfOstot.Open, and when I hit F9 it just hangs and nothing happens. If I turn the debugger off, it neatly displays exception message. Well, I'm running this under windows XP, so that might be enough reason   ::)
(normally I use Ubuntu)


« Last Edit: September 16, 2010, 12:50:19 pm by erkka »

 

TinyPortal © 2005-2018