home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / ExceptMisc / Except6.pas < prev    next >
Pascal/Delphi Source File  |  1998-03-26  |  726b  |  32 lines

  1. function GetInt: Integer;
  2. var
  3.   I, J, K: Integer;
  4. begin
  5.   I := 0;
  6.   J := 10;
  7.   try
  8.     K := J div I;  // Raises an exception
  9.     Result := K;   // Assign function result (doesn't execute)
  10.     ShowMessage('Results: K=' + IntToStr(K));
  11.   except
  12.     Result := 0;   // Assign function result on error
  13.     ShowMessage('Divide error! ' +
  14.      ' I=' + IntToStr(I) +
  15.      ' J=' + IntToStr(J) );
  16.   end;
  17. end;
  18.  
  19. procedure TForm1.Button1Click(Sender: TObject);
  20. var
  21.   K: Integer;
  22.   P: Pointer;
  23. begin
  24.   GetMem(P, 4098);  // Allocate memory resource
  25.   try
  26.     K := GetInt;    // Might cause an exception
  27.   finally
  28.     FreeMem(P, 4098);  // Guaranteed to execute
  29.     ShowMessage('Memory was freed');
  30.   end;
  31. end;
  32.