On Mon, Jul 10, 2017 at 11:19 AM, Sven Barth via fpc-pascal <
fpc-pascal@lists.freepascal.org> wrote:

> Though you can't use the same code block for multiple types.
>
Structured development to the rescue :)

===
program project1;

{$mode delphi}
{$RANGECHECKS on}

uses SysUtils;

function HandlingIt(const e: exception): Integer;
begin
  writeln('something bad has happened:');
  writeln(e.message);
  Result:=0;
end;

function Catch(a,b: byte; oper: char): byte;
begin
  try
    if oper = '+' then Result:=a+b
    else if oper = '/' then Result:=a div b;
  except
    on e: EDivByZero do begin
      writeln('division by zero');
      Result:=0;
    end;
    on e: EIntOverflow do Result:=HandlingIt(e);
    on e: ERangeError do Result:=HandlingIt(e);
  end;
end;

begin
  writeln( catch (1,1,'+'));
  writeln( catch (255,255,'+'));
  writeln( catch (1,0,'/'));
end.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to