Originale-mail to me for new edition

 

Raising and handling exceptions

 

To create an exception object, call the exception class’s constructor within a raise statement. For example,

raise EMathError.Create;

In general, the form of a raise statement is

raise object at address

where object and at address are both optional. If object is omitted, the statement re-raises the current exception; see Re-raising exceptions. When an address is specified, it is usually a pointer to a procedure or function; use this option to raise the exception from an earlier point in the stack than the one where the error actually occurred.

When an exception is raised, that is, referenced in a raise statement it is governed by special exception-handling logic. A raise statement never returns control in the normal way. Instead, it transfers control to the innermost exception handler that can handle exceptions of the given class. (The innermost handler is the one whose try...except block was most recently entered but has not yet exited.)

For example, the function below converts a string to an integer, raising an ERangeError exception if the resulting value is outside a specified range.

function StrToIntRange(const S: string; Min, Max: Longint): Longint;

begin

  Result := StrToInt(S);  // StrToInt is declared in SysUtils

  if (Result < Min) or (Result > Max) then

    raise ERangeError.CreateFmt(

      '%d is not within the valid range of %d..%d',

      [Result, Min, Max]);

end;

Notice the CreateFmt method called in the raise statement. Exception and its descendants have special constructors that provide alternative ways to create exception messages and context IDs.

A raised exception is destroyed automatically after it is handled. Never attempt to destroy a raised exception manually.

Note:          Raising an exception in the initialization section of a unit may not produce the intended result. Normal exception support comes from the SysUtils unit, which must be initialized before such support is available. If an exception occurs during initialization, all initialized units including SysUtils are finalized and the exception is re-raised. Then the exception is caught and handled, usually by interrupting the program.

Try...except statements

Nested exceptions

Try...finally statements

 

Topic groups

 

See also

Declaring exception types

Exception

Exceptions: Overview

The initialization section

 

 

译文

 

引发和处理异常

 

要创建一个异常对象,应在raise语句中调用异常类的构造器,如

raise EMathError.Create;

一般情况下,raise具有如下形式

raise object at address

这里的objectat address都是可选的。如果object被省略,那么语句将再引发当前异常(见再引发异常)。当某个address被指定时,通常是一个指向过程或函数的指针;利用该选项可以从堆栈中较早的点(与错误实际发生的点比较)引发异常。

异常被引发,即异常在raise语句中被引用时,异常由特殊的异常处理逻辑来管理。通常,raise语句从不返回控制。取而代之的是,它把控制传递到最内层的异常处理程序,该处理程序能处理给定类的异常。(最内层的处理程序就是一个try...except块中尚未退出的最近入口。)

例如,下面的函数把一个串转换为整数,如果结果在指定范围之外则引发一个ERangeError异常。

function StrToIntRange(const S: string; Min, Max: Longint): Longint;

begin

  Result := StrToInt(S);  // StrToInt函数在SysUtils单元中定义

  if (Result < Min) or (Result > Max) then

    raise ERangeError.CreateFmt(

      '%d is not within the valid range of %d..%d',

      [Result, Min, Max]);

end;

可以注意到,在raise语句中调用的是CreateFmt方法。Exception类及其后裔类拥有特殊的构造器,为创建异常消息和环境ID提供多于一种途径。

引发的异常在其处理后自动销毁,决不要试图人为销毁一个引发的异常。

注意:       单元的初始化节中引发异常不能达到预期的结果。一般的异常支持来自SysUtils单元,在异常支持可用之前该单元必需被初始化。如果一个异常在初始化过程中发生,那么包括SysUtils单元在内所有初始化过的单元,都会被结束(finalized)并且异常被再引发。这时异常被捕获和处理,通常通过中断程序。

 

Try...except语句

嵌套异常

Try...finally语句

 

主题组

 

相关主题

声明异常类型

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

异常:概述

初始化节