Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

From within a finally block, is it possible to tell an exception has been raised?

like image 486
Zartog Avatar asked Sep 12 '09 21:09

Zartog


2 Answers

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');
like image 167
Uwe Schuster Avatar answered Oct 19 '22 04:10

Uwe Schuster


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.)

like image 27
Mason Wheeler Avatar answered Oct 19 '22 05:10

Mason Wheeler