home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / EXLIST / MAIN.PAS < prev   
Pascal/Delphi Source File  |  1998-03-26  |  2KB  |  94 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, SysUtils, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Buttons, StdCtrls;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     ListBox1: TListBox;
  12.     Button1: TButton;
  13.     Button2: TButton;
  14.     Button3: TButton;
  15.     BitBtn1: TBitBtn;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.     procedure Button3Click(Sender: TObject);
  20.   private
  21.     { Private declarations }
  22.   public
  23. { 1. Declare the OnException event handler }
  24.     procedure NewOnException(Sender: TObject;
  25.       E: Exception);
  26.     { Public declarations }
  27.   end;
  28.  
  29. var
  30.   MainForm: TMainForm;
  31.  
  32. implementation
  33.  
  34. {$R *.DFM}
  35.  
  36. { 2. Implement the OnException event handler }
  37. procedure TMainForm.NewOnException(Sender: TObject;
  38.   E: Exception);
  39. begin
  40.   Application.ShowException(E);
  41.   ListBox1.Items.Add(E.Message);
  42. end;
  43.  
  44. { 3. Assign the event handler to Application.OnException }
  45. procedure TMainForm.FormCreate(Sender: TObject);
  46. begin
  47.   Application.OnException := NewOnException;
  48. end;
  49.  
  50. { Raise a divide-by-zero exception }
  51. procedure TMainForm.Button1Click(Sender: TObject);
  52. var
  53.   I, J, K: Integer;
  54. begin
  55.   I := 0;
  56.   J := 10;
  57.   try
  58.     K := J div I;  { Divide by zero! }
  59.     { The following statement doesn't execute, but it is
  60.       needed so the optimizer in Object Pascal, which notices
  61.       that K isn't used in this procedure, doesn't strip
  62.       out the preceding statement. Smart compiler. }
  63.     ShowMessage('K=' + IntToStr(K));
  64.   except
  65.     raise;
  66.   end;
  67. end;
  68.  
  69. { Raise a file-not-found exception }
  70. procedure TMainForm.Button2Click(Sender: TObject);
  71. var
  72.   T: TextFile;
  73. begin
  74.   AssignFile(T, 'XXXX.$$$');
  75.   Reset(T);  { Open a non-existent file! }
  76.   try
  77.     { You would normally use T here }
  78.   finally
  79.     CloseFile(T);  { For safety's sake }
  80.   end;
  81. end;
  82.  
  83. { Raise an index-out-of-bounds exception }
  84. procedure TMainForm.Button3Click(Sender: TObject);
  85. var
  86.   I: Integer;
  87. begin
  88.   with ListBox1.Items do
  89.   for I := 0 to Count do  { Should be Count - 1! }
  90.     Strings[I] := Uppercase(Strings[I]);
  91. end;
  92.  
  93. end.
  94.