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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trap divide by zero error 2

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
i have this code:
try
c := a / be;

except
on E: Exception do
begin
Showmessage('Exception class name = ' + E.ClassName);
Showmessage('Exception message = ' + E.Message);
end;

when i run it and be is equal to zero, the program does create an error and asks me to break or continue but does not show me the exception messages as it should. why?

thanks.
P
 
The description of this is not quite complete, I'm thinking. But if you are running this in the IDE, it should stop and present an error message and say "use step or run" to continue. Then you should be able to continue by clicking "OK" and then pressing the "run" button again and have the "except" part execute. Works just fine.

I think, though, there is an option somewhere in the IDE that will eliminate the debugger from stopping when it hits an exception.

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  c: integer;
  a: integer;
  be: integer;
begin
  be := 0;
  a := 5;
  try
    c := a div be;  // debugger exception is here.
    ShowMessage(IntToStr(c));
  except
    on E: Exception do
      begin
        Showmessage('Exception class name = ' + E.ClassName + #13#10 +
                    'Exception message = ' + E.Message );
      end;
  end;
end;

HTH.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
you were correct. i was running it from the ide and should have pressed the continue button to see the message. i feel dumb..

thanks.
P
 
Is it just me, but why not just trap this before attempting the division?

Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Sorry Pierrotc, after 3 attempts at writing an answer, without sounding horribly patronising I have given up!

hang on I have an idea

Validation its basic, first thing you should learn.
if be <= 0 then dont muck about with it.
All this exception trapping is chasing after the horse after it ran out of the stable door you left open.
If the likley error is out of your hands, e.g an API Call then its Ok. (if I can't be old school at my age then who can?)



Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Just before you even try to divide, if the value of BE is 0, then skip the code and raise your own custom exception - or handle it however...

Code:
if be = 0 then begin
  c:= 0; //Or whatever default value if be is 0
end else begin
  c:= a / be;
end;


JD Solutions
 
If you were to do a lot of divisions where you are afraid of division by 0, then I would wrap it into a function...

Code:
function DoDivide(Num1, Num2, Default: Double): Double;
begin
  Result:= Default //Set by default, in case Num2 = 0
  if Num2 <> 0 then
    Result:= Num1 / Num2;
end;


JD Solutions
 
Just don't forget that ; at the end of the first line :p I typed that function directly in the website...

JD Solutions
 
yep...saw that...thanks.
The way i am handling that is just to check if the variable i am using to divide is not equal to zero. I should have thought about that instead of trying to implement exceptions...

Appreciate all the inputs..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top