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;
Exception
异常类型的声明和其他类的声明一样。实际上,用任何类的实例作为异常都是可能的,但还是建议异常应起源于在SysUtils单元中定义的标准异常类Exception。
可以通过继承把异常分组到不同的系列。例如,如下SysUtils单元中的声明为数学错误定义了一系列异常类型。
type
EMathError = class(Exception);
EInvalidOp = class(EMathError);
EZeroDivide = class(EMathError);
EOverflow = class(EMathError);
EUnderflow = class(EMathError);
对于上面给出的声明,可以定义单独的EMathError异常处理程序,也能处理EInvalidOp、EZeroDivide、EOverflow、EUnderflow等异常。
异常类中有时还定义域、方法或属性,用来传送关于错误的附加信息。例如,
type
EInOutError = class(Exception)
ErrorCode: Integer;
end;
Exception(详细信息见联机帮助)