• Log in or sign up for an account here: https://www.sythe.org/login

Error handling in Delphi - by joewnn

Six

Guru
Joined
Jan 21, 2007
Sythe Gold
12
Messages
1,482
Delphi error handling is different from vb error handling. In vb, you would normally put at the top, "On error resume next" or "on error goto errhandling". In delphi, there is a whole section for dealing with errors called "except".

In each function with an except section, you will add "try" at the top, to indicate that if you get errors, the program will execute the code in except.

First add a button and a listbox (listbox1) to your form. Open up the button's code and place a "try" after "begin". Write all of your code here that could cause errors. The code I used was simple. Simple add this after try..

Code:
listbox1.Items[1]


Of course, there is no items[1] in the listbox.. unless you added one, so it will definitely cause an error.

Now after all of your code add the "except" line. Then after that add "end;". In between the except and end you add your error handling code.

There are different errors you can look for.

If you want to pick up any error, add this.

Code:
on e:exception do showmessage(e.message); //or any other message you want

Now if you want to look for specific errors, replace exception with the error name. Here is a list of errors from another site.


Code:
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


Here is an example- the code for the listbox that creates an error makes an eStringListError.

Code:
on e:estringlisterror do showmessage('Estringlisterror ' + e.message);


Here is all the sample code (in between begin and end; of the button)


Code:
try
listbox1.Items[1]
except
on e:exception do showmessage(e.message); //or any other message you want
//on e:estringlisterror do showmessage('Estringlisterror ' + e.message);
end;
 
Runescape Gamblingrunescape gambling
Back
Top