Pascal samples. Exception handling.


Example 1

uses
  SysUtils;

procedure ErrorProc;
var
  I: Integer;
begin
  I := 0;
  I := I div I;
end;

procedure TestExcept;
var
  S: String;
  I: Integer;
begin
  S := 'abc';
  try
    ErrorProc;
  except
    on E:EDivByZero do
    begin
      writeln(S);
      S := E.Message;
      writeln(S);
    end;
    else
    begin
      writeln(456);
    end;
  end;
end;

begin
  TestExcept;
  writeln('ok');
end.

Example 2

uses
  SysUtils;

procedure ErrorProc;
var
  I: Integer;
begin
  I := 0;
  I := I div I;
end;

procedure TestFinally;
var
  S: String;
  I: Integer;
begin
  S := 'abc';
  try
    ErrorProc;
  finally
    writeln(S);
  end;
  writeln('not executed');
end;

begin
  try
    TestFinally;
  except
    writeln('ok');
  end;
end.


Copyright © 2006-2017 Alexander Baranovsky. All rights reserved.