We help IT Professionals succeed at work.

Simple Exception Handling in Delphi 4

hnrsoftware
hnrsoftware asked
on
I'm a long-time Pascal/Delphi programmer, but have never used exceptions.  Now that I need them, I can't get the simplest exceptions to work rationally.  Here is a (hopefully) trivial test example

Function Funky(DivBy : integer) : integer;
     begin
     ShowMessage('Going into Funky Routine DivBy = ['+inttostr(DivBy)+']');
     try
        result := 42 div DivBy;
       except
        on EDivByZero do result := 0;
        on Exception  do result := 0;
       else result := 0;
       end; // of try
     ShowMessage('Leaving Funky Routine Result = ['+inttostr(result)+']');
     end;

I have tried every permutatiuon of the above exception clause, and in every case where DivBy is zero, I get the first ShowMessage and then an exception message raised by Delphi - completely ignoring my
attempt at handling the exception and aborting the program.  (I realize that the above example is wildly redundant - I think that the two "on"s and the else should each catch the exception separately.

I hope that I am just missing something terribly obvious.

*** While writing this question, I went back and tried to execute the program outside the IDE just for the heck of it, and the exception handling worked correctly.  Is this all it was? - that the IDE takes over exception handling?  Can this "feature" be turned off?  ***

Thank you.



Comment
Watch Question

Commented:
Delphi will inform you of raised exceptions when they are raised - before they are handled in code.
All you need is go to Debugger Options, Language Exceptions and add EDivByZero (or any other exception classes you want to suppress within IDE) to the 'Exception Types to Ignore' checklistbox.
HTH
TOndrej

Commented:
Err.. the above comment is valid for Delphi 5/6... not sure where it is in Delphi 4 but should be similar, probably Tools\Environment Options? I don't remember, sorry
You've got it right :-)

When running programs within delphi you will always see a exception message from the IDE - but the exception is still handled by your try-except code.

If you don't want exception messages, you must change the default settings : Tools|Debugger Options...

Regards
Peter
hnrsoftwareSenior Software Engineer

Author

Commented:
Thank you all - I love Delphi, but sometimes I find myself pounding my head against a wall.  The debug option is in the same place in D4 and works just fine.  Howard

Explore More ContentExplore courses, solutions, and other research materials related to this topic.