Sunday, August 3, 2008

Exception Handling in Delphi

We know that errors do occur time to time in any application evenif we think we write only perfect code :-) . We cann't avoid the unexpected errors most of the time. If you are a serious coder , you have to think about the error occuring situations and handle these errors. Delphi has a very advanced exception handling to trap these kind of errors and mainly to avoid system crash. Also you can use this to store your data and release memory properly before an exception. If you have an exception handler for a particular piece of code, the control will jump to this handler when any exception occur in that particular code. Following are the keywords available in Delphi for exception handling.

1. Try .. Except .. end

2. Try ... Finally..end

3. Raise

If you have a piece of code in between Try and Except and any exception occur in any of these code , the control will stop executing try .. except block and control will jump to except .. end block. See the following piece of code.

Try
A := B / C;
ShowMessage(IntToStr(A));
Except
A := 0;
ShowMessage('Error occured');
End;

If any error occur in the 2 lines between Try and Except block , control will jump to the block between Except and end. If no error occur in the code between Try and Except , control will jump to the statement just below the end statement. It won't execute the error handler code. In Delphi, all exceptions are derived from the base class called Exception. If you need more information from the exception happened , you have to use the exception class along with Except clause as shown below.

Try
A := B / C;
ShowMessage(IntToStr(A));
Except
On E : Exception do
A := 0;
ShowMessage('Error Message - ' + E.Message);
ShowMessage('Error class name - ' + E.ClassName);
End;

Instead of Exception base class, you can use specific classes like EAccessViolation , EDatabaseError , EConvertError Etc.

try
Result := x / y;
except
on EZeroDivide do
begin
Result := 0,0;
ShowMessage('Divide by zero!');
end;

You have one more kind of exception in Delphi, that's Try..Finally..end . Finally .. end block is a handler which should execute in any case. If you got any exception in the try .. finally block or if you don’t have any exception in try .. finally block also, the finally .. end block will be executed. So this kind of handler can be used to free memory properly so that the memory will be released in any situations.

Frm := TMyClass.Create();
try
Result := IntToStr(A);
Finally
begin
FreeAndNil(Frm);
end;


Raise Exceptions

If you want to raise an exception explicitly , you can use the command Raise.

Delphi exceptions


Exception - Base class
EAbort - Abort without dialog
EAbstractError - Abstract method error
AssertionFailed - Assert call failed
EBitsError - Boolean array error
ECommonCalendarError - Calendar calc error
EDateTimeError - DateTime calc error
EMonthCalError - Month calc error
EConversionError - Raised by Convert
EConvertError - Object convert error
EDatabaseError - Database error
EExternal - Hardware/Windows error
EAccessViolation - Access violation
EControlC - User abort occured
EExternalException - Other Internal error
EIntError - Integer calc error
EDivByZero - Integer Divide by zero
EIntOverflow - Integer overflow
ERangeError - Out of value range
EMathError - Floating point error
EInvalidArgument - Bad argument value
EInvalidOp - Inappropriate operation
EOverflow - Value too large
EUnderflow - Value too small
EZeroDivide - Floating Divide by zero
EStackOverflow - Severe Delphi problem
EHeapException - Dynamic memory problem
EInvalidPointer - Bad memory pointer
EOutOfMemory - Cannot allocate memory
EInOutError - IO error
EInvalidCast - Object casting error
EInvalidOperation - Bad component op
EMenuError - Menu item error
EOSError - Operating system error
EParserError - Parsing error
EPrinter - Printer error
EPropertyError - Class property error#
EPropReadOnly - Invalid property access
EPropWriteOnly - Invalid property access
EThread - Thread error
EVariantError - Variant problem

No comments: