Originale-mail to me for new edition

 

Nested exceptions

 

Code executed in an exception handler can itself raise and handle exceptions. As long as these exceptions are also handled within the exception handler, they do not affect the original exception. However, once an exception raised in an exception handler propagates beyond that handler, the original exception is lost. This is illustrated by the Tan function below.

type

  ETrigError = class(EMathError);

function Tan(X: Extended): Extended;

begin

  try

    Result := Sin(X) / Cos(X);

  except

    on EMathError do

      raise ETrigError.Create('Invalid argument to Tan');

  end;

end;

If an EMathError exception occurs during execution of Tan, the exception handler raises an ETrigError. Since Tan does not provide a handler for ETrigError, the exception propagates beyond the original exception handler, causing the EMathError exception to be destroyed. To the caller, it appears as if the Tan function has raised an ETrigError exception.

 

Topic groups

 

See also

Raising and handling exceptions

 

 

译文

 

嵌套异常

 

执行在异常处理程序中的代码可以自身引发和处理异常。当这些异常也在异常处理程序中被处理时,不影响最初的异常。不过,一旦在异常处理程序中引发的异常传播到处理程序之外,最初的异常就丢失了。下面通过Tan函数举例说明。

type

  ETrigError = class(EMathError);

function Tan(X: Extended): Extended;

begin

  try

    Result := Sin(X) / Cos(X);

  except

    on EMathError do

      raise ETrigError.Create('Invalid argument to Tan');

  end;

end;

如果在函数Tan的执行中出现了EMathError异常,那么异常处理程序会引发ETrigError异常。因为Tan函数未提供对ETrigError异常的处理程序,所以异常将传播到最初的异常处理程序之外,最终导致EMathError异常被销毁。对于调用者来说,就象Tan函数引发了一个ETrigError异常。

 

主题组

 

相关主题

引发和处理异常