[fpc-pascal] using exceptions

Marcos Douglas md at delfire.net
Thu Dec 22 19:29:49 CET 2011


On Thu, Dec 22, 2011 at 4:17 PM, Anton Shepelev <anton.txt at gmail.com> wrote:
> Marcos Douglas:
>
>> It's  not  difficult,  but is boring have always a
>> parameter or function return to return  an  error,
>> and  have  to  verify  if error then... if not er-
>> ror...  if error and error then... etc
>
> If you mean having to write nested IFs to check  all
> the  errors, then there are several ways to make the
> error-checking in several calls "linearized":
>
>    http://www.digipedia.pl/usenet/thread/1169/284/

No, I mean is better use try-except than nested IFs/GOTO/etc.

> I prefer using GOTO statements:
>
>    begin
>      Result := false; // Success flag
>      [...]
>      errMsg := errorOne;
>      if not procOne  (...) then GOTO ERROR;
>      errMsg := errTwo;
>      if not procTwo  (...) then GOTO ERROR;
>      errMsg := errThree;
>      if not procThree(...) then GOTO ERROR;
>      [...]
>      Result := true
>    ERROR:
>    end
>
> This way, the funtion returns an error  flag  and  a
> correspoinding  message, and I prefer it to throwing
> and catching Exceptions.   I  even  wrap  exception-
> based  code  using external libraries in such proce-
> dures to have an easier and cleaner error handling.
>
> Also see:
>
>    http://www.theregister.co.uk/2006/01/11/exception_handling/

Thanks for the tips... but, IMHO, the code above would be more simpler
like that:

begin
    Result := False; // Success flag
    [...]
    procOne;
    procTwo;
    procThree;
    [...]
    Result := True;
  except
     on e: Exception do
       ShowMessage(e.Message);
  end;
end;

Marcos Douglas



More information about the fpc-pascal mailing list