Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an error code with Halt(n) from an Exception block with D2007?

Update: It seems to be specific to D2007. It works in D2010 like it worked in older version.

I would like to return an exit code depending on the type of Exception caught in the Eception Handler block like:

program test;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  Exitcode: Integer;
begin
  Writeln('Enter error code:');
  Readln(Exitcode);
  try
    raise EExternal.Create('sdsdkfjh');
  except
    on E:EExternal do
    begin
      Writeln(E.Classname, ': ', E.Message);
      Halt(Exitcode);
    end;
  end;
end.

Unfortunately in D2007, calling Halt(n) from an Exception block always returns an Exit code 1, no matter what you pass to Halt().

Apparently because exiting from an Exception handler calls Finalize, which clears the pending (non Abort) Exceptions, calling SysUtils.ExceptHandler:

procedure ExceptHandler(ExceptObject: TObject; ExceptAddr: Pointer); far;
begin
  ShowException(ExceptObject, ExceptAddr);
  Halt(1); // <= @#$##@#$!
end;

And no matter what exit code I wanted I get that Halt(1)!

So the question is:
How can I simply return the desired Exit code depending on which Exception was raised?

like image 306
Francesca Avatar asked Aug 04 '10 19:08

Francesca


3 Answers

Will this work?

NeedHalt := False;
try
  raise EExternal.Create('sdsdkfjh');
except
  on E:EExternal do
  begin
    Writeln(E.Classname, ': ', E.Message);
    NeedHalt := True;
  end;
end;
if NeedHalt then
  Halt(Exitcode); 

Or this?

try
  raise EExternal.Create('sdsdkfjh');
except
  on E:EExternal do
  begin
    Writeln(E.Classname, ': ', E.Message);
    AcquireExceptionObject;
    Halt(Exitcode); 
  end;
end;

Anyway: it's a bug in D2007, which was fixed in D2010.

like image 199
Alex Avatar answered Oct 08 '22 15:10

Alex


Actually... it seems to work as intended....

I used your code...

program test1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  Exitcode: Integer;
begin
  Writeln('Enter error code:');
  Readln(Exitcode);
  try
    raise EExternal.Create('sdsdkfjh');
  except
    on E:EExternal do
    begin
      Writeln(E.Classname, ': ', E.Message);
      Halt(Exitcode);
    end;
  end;
end.

Compiled in in Delphi 5, then ran it in a DOS box under XP...

C:\>test1
Enter error code:
111
EExternal: sdsdkfjh

C:\>echo %errorlevel%
111

C:\>

Note that DOS Error Levels are restricted to the range of 0 to 65535. Echoing %errorlevel% is the quickest way to see the error level.

Don't forget that reading the errorlevel clears it.

like image 38
Mike Warot Avatar answered Oct 08 '22 14:10

Mike Warot


If you want to immediately abort the program without any cleanup, and return an exit code, try ExitProcess. See the article for a few caveats on using ExitProcess, though.

like image 2
Mason Wheeler Avatar answered Oct 08 '22 15:10

Mason Wheeler