Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 6

Chapter 03 - Project TryFinally

Project Structure

TryFinally.dpr
program TryFinally;

uses
  Forms,
  TryForm in 'TryForm.pas' {Form1};

{$R *.RES}

begin
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
TryForm.pas
unit TryForm;

interface

uses
  SysUtils, Windows, Messages, Classes,
  Graphics, Controls, Forms, Dialogs, StdCtrls;
type
  TForm1 = class(TForm)
    BtnWrong: TButton;
    BtnTryFinally: TButton;
    BtnTryTry: TButton;
    procedure BtnWrongClick(Sender: TObject);
    procedure BtnTryFinallyClick(Sender: TObject);
    procedure BtnTryTryClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BtnWrongClick(Sender: TObject);
var
  I, J: Integer;
begin
  Screen.Cursor := crHourglass;
  J := 0;
  // long (and wrong) computation...
  for I := 1000 downto 0 do
    J := J + J div I;
  MessageDlg ('Total: ' + IntToStr (J),
    mtInformation, [mbOK], 0);
  Screen.Cursor := crDefault;
end;

procedure TForm1.BtnTryFinallyClick(Sender: TObject);
var
  I, J: Integer;
begin
  Screen.Cursor := crHourglass;
  J := 0;
  try
    // long (and wrong) computation...
    for I := 1000 downto 0 do
      J := J + J div I;
    MessageDlg ('Total: ' + IntToStr (J),
      mtInformation, [mbOK], 0);
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TForm1.BtnTryTryClick(Sender: TObject);
var
  I, J: Integer;
begin
  Screen.Cursor := crHourglass;
  J := 0;
  try try
    // long (and wrong) computation...
    for I := 1000 downto 0 do
      J := J + J div I;
    MessageDlg ('Total: ' + IntToStr (J),
      mtInformation, [mbOK], 0);
  finally
    Screen.Cursor := crDefault;
  end;
  except
    on E: EDivByZero do
    begin
      // re-raise the exception with a new message
      raise Exception.Create ('Error in Algorithm');
    end;
  end;
end;

end.
TryForm.dfm
object Form1: TForm1
  Left = 214
  Top = 125
  Width = 487
  Height = 126
  ActiveControl = BtnWrong
  Caption = 'TryFinally'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object BtnWrong: TButton
    Left = 40
    Top = 25
    Width = 113
    Height = 49
    Caption = 'Wrong'
    TabOrder = 0
    OnClick = BtnWrongClick
  end
  object BtnTryFinally: TButton
    Left = 184
    Top = 25
    Width = 113
    Height = 49
    Caption = 'Try Finally'
    TabOrder = 1
    OnClick = BtnTryFinallyClick
  end
  object BtnTryTry: TButton
    Left = 328
    Top = 24
    Width = 113
    Height = 49
    Caption = 'Try Try'
    TabOrder = 2
    OnClick = BtnTryTryClick
  end
end