hnrsoftware
asked on
Simple Exception Handling in Delphi 4
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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