Originale-mail to me for new edition

 

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;

 

Topic groups

 

See also

About class types

Exception

Exceptions: Overview

 

 

译文

 

声明异常类型

 

异常类型的声明和其他类的声明一样。实际上,用任何类的实例作为异常都是可能的,但还是建议异常应起源于在SysUtils单元中定义的标准异常类Exception

可以通过继承把异常分组到不同的系列。例如,如下SysUtils单元中的声明为数学错误定义了一系列异常类型。

type

  EMathError = class(Exception);

  EInvalidOp = class(EMathError);

  EZeroDivide = class(EMathError);

  EOverflow = class(EMathError);

  EUnderflow = class(EMathError);

对于上面给出的声明,可以定义单独的EMathError异常处理程序,也能处理EInvalidOpEZeroDivideEOverflowEUnderflow等异常。

异常类中有时还定义域、方法或属性,用来传送关于错误的附加信息。例如,

type EInOutError = class(Exception)

    ErrorCode: Integer;

  end;

 

主题组

 

相关主题

关于类类型

Exception(详细信息见联机帮助)

异常:概述