[fpc-pascal] using exceptions

Anton Shepelev anton.txt at gmail.com
Thu Dec 22 19:17:13 CET 2011


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/

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/

Anton



More information about the fpc-pascal mailing list