Declaring Exception Types Exception types are declared just like other classes. In fact, it is possible to use an instance of any class as an
exception, but it is recommended that exceptions be derived from the Exception class defined in
SysUtils
.
You can group exceptions into families using inheritance. For example, the following declarations in
SysUtils
define a family of exception types for math errors.
type
EMathError = class(Exception);
EInvalidOp = class(EMathError);
EZeroDivide = class(EMathError);
EOverflow = class(EMathError);
EUnderflow = class(EMathError);
Given these declarations, you can define a single EMathError exception handler that also handles EInvalidOp,
EZeroDivide, EOverflow, and EUnderflow.
Exception classes sometimes define fields, methods, or properties that convey additional information about the error.
For example,
type EInOutError = class(Exception)
ErrorCode: Integer;
end;