Managing Errors

The Except keyword is used to mark the start of a block of statements that handle an exception in a Try clause. If the Except block can handle the exception, then the program is not terminated.

If the Try clause generates an exception the Except clause is executed. This is used to take alternative action when something unexpected goes wrong. The except clause cannot determine the error type however.

 

 

 

Structure:

 Try

         //Statement

 Except

 //Statement

 End;

 

 

 

Example:

 

var  number, zero : Integer;

begin

 // Try to divide an integer by zero - to raise an exception

 Try

   zero   := 0;

   number := 1 div zero;

   ShowMessage('number / zero = '+IntToStr(number));

 Except

   ShowMessage('Unknown error encountered');

 end;

end;