Article

From:
To:
Kristofer Skaug
Subject:
Re: Exceptions vs Error codes
Newsgroup:
borland.public.delphi.objectpascal

Re: Exceptions vs Error codes

"Barry Kelly" <✉eircom.net> wrote 

> > My point is that exceptions are unified; they all descend from
> > Exception, they have a standardized handling mechanism.

> > A programmer must explicitly dismiss an exception, while an error code
> > can be ignored by default.

While (slightly) in the pro-exception camp, I must mention that it
is quite possible to have unified, non-ignorable error objects.

I wrote the proof of concept below, which :
- Represents errors with type Exception, and is therefore unified. - Raises an exception if the last reference to an unhandled error   disappears, and therefore cannot be ignored.
Moreover, error objects can be suspended and composed, while raised exceptions cannot really.
--- Raoul
{ Usage }
function DoSomething : IException; begin   Result := Ok; end;
function DoSomethingElse : IException; begin   Result := TException.Create(Exception.Create('AProblem')); end;
function DoMore : IException; begin   Result := DoSomething;   if Result.Ok then     Result := DoSomethingElse; end;
procedure TForm1.Button1Click(Sender: TObject); var   Error : IException; begin   DoSomething;   Error := DoMore;   if not Error.Ok then   begin     // Handle error in Error.E
Error.Handled := true; // Must declare error handled. Otherwise, exception is raised.
  end;
end;

{ interface & implementation }
interface   type   IException = interface ['{3125F638-5ADE-4EFB-A580-4EE129F7D840}']     function E : Exception;     function Ok : Boolean;     function GetHandled : Boolean;     procedure SetHandled(Handled : Boolean);     property Handled : Boolean read GetHandled write SetHandled;   end;
  TException = class(TInterfacedObject, IException)   private     FException : Exception;     FHandled : Boolean;   public     constructor Create(E : Exception);     procedure FreeInstance; override;     function GetHandled : Boolean;     procedure SetHandled(Handled : Boolean);     function E : Exception;     function Ok : Boolean;   end;
  TNoException = class(TException)   public     constructor Create;   end;
  function Ok : IException;
implementation
var   SOk : IException;
function Ok : IException; begin   if not assigned(SOk) then     SOk := TNoException.Create;   Result := SOk; end;
{ TException }
constructor TException.Create(E: Exception); begin   inherited Create;   FException := E; end;
function TException.E: Exception; begin   Result := FException; end;
procedure TException.FreeInstance; var   Handled : Boolean;   E : Exception; begin   Handled := FHandled;   E := FException;   try     inherited;   except     E.Free;     raise;   end;   if Handled then     E.Free   else     raise E; end;
function TException.GetHandled: Boolean; begin   Result := FHandled end;
function TException.Ok: Boolean; begin   Result := Self is TNoException end;
procedure TException.SetHandled(Handled: Boolean); begin   FHandled := Handled end;
{ TNoException }
constructor TNoException.Create; begin   inherited Create(Exception.Create(''));   SetHandled(true); end;
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Wed, 09 Oct 2024 15:24:14 UTC
Copyright © 2009-2024
HREF Tools Corp.