E
E
Evgeny Ivanov2016-12-21 07:11:54
Delphi
Evgeny Ivanov, 2016-12-21 07:11:54

Why is there no exception handling?

The simplest code. The message - An exception has occurred does not appear. Any code in the except section is not executed.
If you take out the code c:=a div b; outside of the try-except, the program will report Division by zero.
Why is there no exception handling?

procedure TForm1.Button2Click(Sender: TObject);
var
a,b,c:integer;
begin
a:=1;
b:=0;
try
c:=a div b;
except
ShowMessage('Возникло исключение.'); //Это сообщение не появляется
end;
end;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-12-21
@logpol32

For me (Delphi 7), your code is apparently destroyed by the optimizer, because it does not use the value of the variable c, it’s too lazy to disassemble, but it looks like this, because if you make it used, it works:

procedure TForm1.Button1Click(Sender: TObject);
var
  a, b, c: integer;
begin
a := 5;
b := 0;
try
  c := a div b;
  ShowMessage(FloatToStr(c)); // --
except
  ShowMessage('test');
end;
end;

Of course, you need to run it outside the Delphi debugger, otherwise it will first show its standard error message.

X
xmoonlight, 2016-12-21
@xmoonlight

except
on EZeroDivide do ShowMessage('Возникло исключение.');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question