17.5 Exception classes

The sysutils unit contains a great deal of exception handling. It defines the base exception class, Exception

Exception = class(TObject)  
  fmessage : string;  
  fhelpcontext : longint;  
  constructor create(const msg : string);  
  constructor createres(indent : longint);  
  property helpcontext : longint read fhelpcontext write fhelpcontext;  
  property message : string read fmessage write fmessage;  
ExceptClass = Class of Exception;

And uses this declaration to define quite a number of exceptions, for instance:

{ mathematical exceptions }  
EIntError = class(Exception);  
EDivByZero = class(EIntError);  
ERangeError = class(EIntError);  
EIntOverflow = class(EIntError);  
EMathError = class(Exception);

The SysUtils unit also installs an exception handler. If an exception is unhandled by any exception handling block, this handler is called by the Run-Time library. Basically, it prints the exception address, and it prints the message of the Exception object, and exits with an exit code of 217. If the exception object is not a descendent object of the Exception object, then the class name is printed instead of the exception message.

It is recommended to use the Exception object or a descendant class for all raise statements, since then the message field of the exception object can be used.