简体   繁体   中英

How can I tell if I'm in a Delphi exception stack?

从finally块中,是否有可能告诉异常已被提出?

You could check if ExceptObject or ExceptAddr are assigned. In the VCL source this is done for exam. in GIFImg.pas or jpeg.pas.

The following code should output

ExceptObject <> nil
ExceptObject = nil

and if you remove the exception then of course

ExceptObject = nil
ExceptObject = nil

  try
    try
      raise Exception.Create('Just an exception');
    finally
      if ExceptObject <> nil then
        WriteLn('ExceptObject <> nil')
      else
        WriteLn('ExceptObject = nil');
    end;
  except

  end;
  if ExceptObject <> nil then
    WriteLn('ExceptObject <> nil')
  else
    WriteLn('ExceptObject = nil');

This is sort of a hack, but you could try calling AcquireExceptionObject. If you're in an exception state, you'll get a return value, otherwise you'll get nil.

(If you did get one, make sure to call ReleaseExceptionObject afterwards.)

AFAIK this can only be achieved with nested try statements :

Try
  Try
    ...
  Except
    ...
  End;
Finally
  ...
End

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM