6
:
Delphi . :
:
4- .
3- .
.
( ).
:
. = . PRINT ,
Microsoft Excel. ( CreateOleObject(Excel.Application) Excel. , .)
var
WB,EX:OleVariant;
begin
try
EX:= CreateOleObject('Excel.Application');
WB:=EX.Workbooks.Add($FFFFEFB9);
except
LogError(' Excel.');
Exit;
end;
. .
:
. 1
Exception. 2
. 3
Object Pascal 4
try..except 4
try...finally. 5
. 6
. 7
- , .
Delphi , , , . - ? , . Object Pascal .
, (tryexcept). , VCL , .
Exception
. Delphi SYSUTILS.PAS Exception. . .
Exception = class (TObject)
Private
FMessage: string;
FHelpContext: Integer;
Public
constructor Create(const Msg: string);
constructor CreateFmt(const Msg: string; const Args: array of const);
constructor CreateRes(Ident: Integer); overload;
constructor CreateRes(ResStringRec: PResStringRec); overload;
constructor CreateResFmt(Ident: Integer; const Args: array of const); overload;
|
|
constructor CreateResFmt(ResStringRec: PResStringRec; const Args: array of const); overload;
constructor CreateHelp(const Msg: string; AHelpContext: Integer);
constructor CreateFmtHelp(const Msg: string; const Args: array of const;
AHelpContext: Integer);
constructor CreateResHelp(Ident: Integer; AHelpContext: Integer); overload;
constructor CreateResHelp(ResStringRec: PResStringRec; AHelpContext: Integer); overload;
constructor CreateResFmtHelp(ResStringRec: PResStringRec; const Args: array of const; AHelpContext: Integer); overload;
constructor CreateResFmtHelp(Ident: Integer; const Args: array of const;
AHelpContext: Integer); overload;
property HelpContext: Integer read FHelpContext write FHelpContext;
property Message: string read FMessage write FMessage;
end;
ExceptClass = class of Exception;
Exception, , ( Res), ( Fmt), ( Help).
, Fmt, , Format:
If MemSize > Limit then
raise EOutOfMemory.CreateFmt('Cannot allocate more than %d bytes',[Limit]);
Exception , /, . .
, Exception , . Exception , EZeroDivide.
1. ( EIntError)
EDivByZero | ( ) |
ERangeError | |
EIntOverflow |
2. ( EMathError)
EInvalidOp | |
EZeroDivide | |
EOverflow | |
EUnderflow | |
EInvalidArgument |
. , Exception , - .
raise, Exception. :
raise EMathError.Create(' ');
:
var E: EMathError;
Begin
E:= EMathError.Create ');
raise E;
end;
raise try..except (. ).
if = 0 then
raise EDivByZero.Create(' ')
Else
:= /;
, . . , . . , VCL , , ( ?) EListError, EInvalidGraphic, EPrinter . .
|
|
Exception , . , . , Message. , . , EinOutError ( /) ErrorCode, , . .
Try
FileOpen('ñ:\myfile.txt', fmOpenWrite);
Except
On E: EinOutError do
Case E.ErrorCode of
ERROR_FILE_NOT_FOUND {=2}: ShowMessage(' !');
ERROR_ACCESS_DENIED {=5}: ShowMessage(' !');
ERROR_DISK_FULL {=112}: ShowMessage (' !');
end;
end;
, EInOutError , {$IOCHECKS ON} ( {$I+}). IOResult ( Turbo Pascal) .
Object Pascal
Object Pascal tryexcept tryfinally. , except finally. , , . , , .
, .
try..except
try..except. :
Try
<>;
<>;
...
Except
On EException1 do
<>; { EException1}
On EException2 do
<>;
...
Else
<0> { }
end;
try. . except . , try ; , end.
, , except end. , ondo, . (on...), , (do...).
U:= 220.0;
R:= 0;
Try
I:= U / R;
Except
on EZeroDivide do MessageBox(' !');
end;
ifthen tryexcept, , . , , , , - tryexcept .
, . , on, : , , .
|
|
, .
Try
i:=l;j:=0;
k:=i div j;
Except
on EIntError do ShowMessage('IntError');
on EDivByZero do ShowMessage('DivByZero');
end;
, (EDivByZero), , EIntError. ondo , .
, , , else. , . try..except, .
try...finally
try..except try..finally. , . try..finally :
Try
<>
<>
...
Finally
<>
...
end;
: , finally, .
try . , , finally. , try finally , finally, . , try , ( , , . .). , , finally, . try...finally, , .
: . try...finally . "" .
, , . :
Try
AllocatelstResource;
Try
Allocate2ndResource;
SolveProblem;
Finally
Free2ndResource;
end;
Finally
FreelstResource;
end;
, :
var i,j,k: Integer;
Begin
i:= Round(Random);
j:= 1 - i;
Try
k:= 1 div i;
Try
k:= 1 div j;
Except
On EDivByZero do
ShowMessage(' 1: j=0');
end;
Except
On EDivByZero do
ShowMessage(' 2: i=0');
end;
end;
. ( finally), - ( except).