Metropoli BBS
VIEWER: xcptdemo.pas MODE: TEXT (CP437)
{█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█}
{█                                                       █}
{█      Virtual Pascal Examples. Version 1.10            █}
{█      Exceptions demonstration example                 █}
{█      ─────────────────────────────────────────────────█}
{█      Copyright (C) 1996 fPrint UK Ltd                 █}
{█                                                       █}
{▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀}

program XcptDemo;

{&X+,Delphi+}

// This example demonstrates a number of uses for Delphi style exceptions.
// The example can be compiled using the command-line compiler of Delphi v2.0.
// Write DCC32.EXE XCPTDEMO /CC to compile with Delphi v2.0.

// Size of executables: VP/2   - 38kB
//                      Delphi - 51kB

Uses
{$IFDEF OS2}
  Os2Base,
{$ENDIF}
  Crt, SysUtils;

type
  EParameterError = class(Exception);

{$IFDEF VPDEMO}
  {&Dynamic VP11Demo.Lib}
{$ENDIF}

//
// Guarded GotoLine procedure that raises an exception for an invalid parameter
//
procedure gGotoLine( Line : Integer );
begin
  If ( Line in [1..20] ) then
    GotoXY( 1, Line )
  else
    raise EParameterError.CreateFmt( 'Invalid line number: %d', [Line] );
end;

//
// Test procedure, using gGotoLine
//
procedure TestLine( Line : Integer);
begin
  try
    gGotoLine( Line );
    Write( Format( 'Now at Line %d ', [Line] ) );
  except
    on E:Exception do
      begin
        GotoXY( 1, 22 );
        Writeln( 'Error : ',E.Message );     // Write error message
        raise;                             // Re-raise the exception
      end;
  end;
end;

procedure Test;
begin
  try
    TestLine( 10 ); // OK
    TestLine( 3  ); // OK
    TestLine( -1 ); // Invalid; generates exception
  except
    on E:EParameterError do
      // Handle invalid parameter case
      Writeln( 'Parameter error in some call' );
    else
      // handle any other exception
      Writeln( 'Unexpected exception occured' );
  end;
end;

begin
  ClrScr;
  WriteLn('Virtual Pascal Exceptions Demo  Version 1.10 Copyright (C) 1996 fPrint UK Ltd');
  {$IFDEF OS2}
  PopupErrors := False;
  {$ENDIF}
  Test;
end.



[ RETURN TO DIRECTORY ]