定义异常类型

异常处理

uses System.SysUtils;

type
  //异常类继承自 Exception,
  //名称以 E 开头
  ESimpleException = class(Exception);

  ERecommendExc = class(Exception)
  public
    //Create(Msg) is inherited;
    //可添加其他任意成员
    ErrorCode: Integer;
    constructor Create(const Msg: string;
      ACode: Integer);
  end;

constructor ERecommendExc.Create(
  const Msg: string; ACode: Integer);
begin
  inherited Create(Msg);
  ErrorCode := ACode;
end;

begin
  try
    raise ERecommendExc.Create('Oops!', 42);
  except
    on E: ERecommendExc do
      WriteLn(E.Message, ', code ',
        E.ErrorCode);
  end;
end.