Exception( unit err.pas ) TObject
Exception = class( TObject )
Exception class. In KOL, there is a single exception class is used. Instead of inheriting new exception classes from this ancestor, an instance of the same Exception class should be used. The difference is only in Code property, which contains a kind of exception.
type TError =( e_Abort, e_Heap, e_OutOfMem, e_InOut, e_External, e_Int, e_DivBy0, e_Range, e_IntOverflow, e_Math, e_Math_InvalidArgument, e_InvalidOp, e_ZeroDivide, e_Overflow, e_Underflow, e_InvalidPointer, e_InvalidCast, e_Convert, e_AccessViolation, e_Privilege, e_StackOverflow, e_CtrlC, e_Variant, e_PropReadOnly, e_PropWriteOnly, e_Assertion, e_Abstract, e_IntfCast, e_InvalidContainer, e_InvalidInsert, e_Package, e_Win32, e_SafeCall, e_License, e_Custom, e_Com, e_Ole, e_Registry );
Main error codes. These are to determine which exception occure. You can use e_Custom code for your own exceptions.
type Exception = class( TObject )
Exception class. In KOL, there is a single exception class is used. Instead of inheriting new exception classes from this ancestor, an instance of the same Exception class should be used. The difference is only in Code property, which contains a kind of exception.
Exception properties
Text string, containing descriptive message about the exception.
property Code: TError;
Main exception code. This property can be used to determine, which exception occur.
This code is to detailize error. For Code = e_InOut, ErrorCode contains more detail description of input/output error. For e_Custom, You can assign it to any value You want.
property ExceptionRecord: PExceptionRecord;
This property is only for e_External exception.
Custom defined pointer. Use it in your custom exceptions.
Exception methods
constructor Create( ACode: TError; const Msg: string );
Use this constructor to raise exception, which does not require of argument formatting.
constructor CreateFmt( ACode: TError; const Msg: string; const Args: array of const );
Use this constructor to raise an exception with formatted Message string. Take into attention, that Format procedure defined in KOL, uses API wvsprintf function, which can understand a restricted set of format specifications.
constructor CreateCustom( AError: DWORD; const Msg: String );
Use this constructor to create e_Custom exception and to assign AError to its ErrorCode property.
constructor CreateCustomFmt( AError: DWORD; const Msg: String; const Args: array of const );
Use this constructor to create e_Custom exception with formatted message string and to assign AError to its ErrorCode property.
constructor CreateResFmt( ACode: TError; Ident: Integer; const Args: array of const );
destructor
Exception events
property OnDestroy: TDestroyException;
This event is to allow to do something when custom Exception is released.
Exception tasks
With err unit, it is possible to use all capabilities of Delphi exception handling almost in the same way as usual. The difference only in that the single exception class should be used. To determine which exception occure, use property Code. So, code to handle exception can be written like follow:
try
...
except on E: Exception do
case E.Code of
e_DivBy0: HandleDivideByZero;
e_Overflow: HandleOverflow;
...
end;
end;
To raise an error, create an instance of Exception class object, but pass a Code to its constructor:
var E: Exception;
...
E := Exception.Create( e_Custom, 'My custom exception' );
E.ErrorCode := MY_MAGIC_CODE_FOR_CUSTOM_EXCEPTION;
raise E;