E06 (Exception)
1 (Report) : { $I- } { I/O } Assign(InFile, InputName); Reset(InFile); { $I+ } { I/O } if IOResult <> 0 then { }; (Exception) 2
2 (Settling State) Post OnValidate BeforePost Post Settling State AfterPost Settling State, OnValidate BeforePost AfterPost settling state 3
Settling State OnValidate Raises an exception BeforePost Post AfterPost Settling State (BeforePost, Post, AfterPost) settling state ( ) 4
3 2 31 finally finally <statement>; <statement>; finally <statement>; <statement>; finally finally finally finally finally end end finally finally, finally finally, finally 5
(memory, graphics handles ),,, (leak),, graphics handle handle, graphics handle finally About About : procedure TfrmMainmntmAboutClick(Sender: TObject); frmabout := TfrmAboutcreate( Application ); frmaboutshowmodal; frmaboutfree;, Free (, graphics ) : procedure TfrmOrderEnMainmntmAboutClick(Sender: TObject); frmabout := TfrmAboutcreate( Application ); frmaboutshowmodal; finally frmaboutfree; 6
//finally, finally Assign( TextFile, TextFileName ); reset( TextFile ); readln( TextFile, buf ); finally { } CloseFile( TextFile ); {finally} finally cleanup 32 except except : < statement >; < statement >; except on ExceptionType1 do <statement(s)>; 7
on ExceptionType2 do {except} <statement(s)>; except : result := x/y; except on EZeroDivide do MessageDlg( Division by zero, mterror, [mbok], 0 ); on Eunderflow do MessageBeep( 0 ); MessageDlg( Numbers too small for valid results, {on Eunderflow} mterror, [mbok], 0 ); finally, except end, except (Exception) T E " " except except except, except 8
: function GetRatio( x, y : integer ) : Double; Result := x/y; except on EZeroDivide do Result := 00; ShowMessage( Cannot divide by zero ); {EZeroDivide} on EUnderFlow do Result := 00; ShowMessage( Difference is too great ); {EUnderFlow} on EMathError do Result := 00; ShowMessage( General math error ); {EMathError} {except} {procedure} y 0 ( ) math Project Browser : 9
Zero Dvide UnderFlow MathError 2 (math error), except EMathError 2 Exception 10
4 Propagation Propagating ' ' call stack Application call stack propagation Application GetRatio OutOfMemory, 5 Raise raise Raise 2 raise, propagation raise 0, propagation Raise propagation function GetAverage( Sum : integer; NumItems : word ) : Double; result := sum / NumItems; except 11
on EDivByZero do result := 00; // raise; {EDivByZero} {except} // Raise Simulation, EdivByZero, : raise EDivByZerocreate( 0 ); Raise raise 6 finally except finally except : function GetAverageFromFile( FileName : string ) : Double; var inputfile : Text; buf : string; sum, numitems : integer; sum := 0; numitems := 0; assignfile( inputfile, FileName ); reset( inputfile ); 12
while ( not eof( inputfile ) ) do readln( inputfile, buf ); sum := StrToInt( buf ); inc( numitems ); result := sum / numitems; except on EDivByZero do result := 00; raise; {EDivByZero} {except} finally CloseFile( inputfile ); {finally} numitems 0, 00 propagation finally finally 7 71 Exception : type EInvalidBalance = class( Exception ); 13
Raise EInvalidBalancecreate(! ); 72 Instance : except on E : EInvalidBalance do ShowMessage( Error message: + Emessage );, E scoping 14
propagation, out of scope 73 (Silent Exception) (silent exception) propagation EAbort EAbort : raise EAbortcreate( Invisible message ); Abort EAbort EAbort Abort : if not customerisvalidbalance( -50 ) then Abort; 15
8 Application Application propagation Application Application HandleException Application propagation HandleException ShowException ShowException HandleException OnException Application OnException Application Object Inspector,, 2 TObject Sender E Application : procedure TfrmAppExcepCustomExceptionHandler( Sender : TObject; E : Exception ); if E is EInvalidBalance then ShowMessage( 'Handling Invalid Balance exception' ) else applicationshowexception( E ); procedure TfrmAppExcepFormCreate(Sender: TObject); applicationonexception := CustomExceptionHandler; 16
(ShowException ) 17
9 91 EDatabaseError EDa tabaseerror EDatabaseError EDatabaseError EDatabaseError Cancel : repeat // Cancel tblconntestactive:= True; // tblconntest Break; // except on EDatabaseError do // OK if MessageDlg('Could not open table', mterror, [mbok, mbcancel], 0) <> mrok then raise; // Cancel // except until False; EDatabaseError BDE DBEngine 92 OnPostError, OnEditError, OnDeleteError OnPostError, OnEditError, OnDeleteError TTable, TQuerie, TStoredProcedure : 18
procedure TfrmAppExceptblNamePostError(DataSet: TDataSet; E: EDatabaseError; var Action: TDataAction); : DataSet TDataSet E EDatabaseError Action TDataAction 3 : dafail, daabort, dare Action dafail EDatabaseError daabort EDatabaseError dare Action dafail daabort 93 EDBEngineError EDBEngineError EDatabaseError Errors ErrorCount BDE, BDE 3 BDE 3 Errors ErrorCount BDE Errors TDBError TDBError : ErrorCode Category BDE ( ) ErrorCode 19
SubCode NativeError Message 0, BDE BDE 94 BDE BDE DCU Delphi5 DOC BDEINT BDE BDE 16 : ERRBASE_NONE = 0; { No error } ERRBASE_SYSTEM = $2100; { System related (Fatal Error) } ERRBASE_NOTFOUND = $2200; { Object of interest Not Found } ERRBASE_DATACORRUPT = $2300; { Physical Data Corruption } offset : ERRCODE_SYSFILEOPEN = 1; { Cannot open a system file } ERRCODE_SYSFILEIO = 2; { I/O error on a system file } ERRCODE_SYSCORRUPT = 3; { Data structure corruption } ERRCODE_NOCONFIGFILE = 4; { Cannot find config file } offset : DBIERR_SYSFILEOPEN = (ERRBASE_SYSTEM + ERRCODE_SYSFILEOPEN); DBIERR_SYSFILEIO = (ERRBASE_SYSTEM + ERRCODE_SYSFILEIO); DBIERR_SYSCORRUPT = (ERRBASE_SYSTEM + ERRCODE_SYSCORRUPT); DBIERR_NOCONFIGFILE = (ERRBASE_SYSTEM + ERRCODE_NOCONFIGFILE); 20
DBIERR_ BDE BDE BDE BDE : tblconnectactive := true; except on E : EDBEngineError do MessageDlg( 'Can''t find table', mterror, [mbok], 0 );? BDE : tblconnectactive := true; except on E : EDBEngineError do case Eerrors[0]errorCode of DBIERR_OSENOPATH : MessageDlg( 'Invalid Path', mterror, [mbok], 0 ); DBIERR_OSENOENT : MessageDlg( 'File not Found', mterror, [mbok], 0 ); else raise; //case BDE BDE BDE 21