Thursday, July 17, 2008

Exceptionerror Handling An Integral Part Of Programming

Writen by Duane Hennessy

Exception handling is an essential part of the programming experience, it polishes the application and is a bulwark against somewhat forgivable oversight. We most likely will not cover every situation in the first build of an application but at least we can include the ability to handle unforeseen runtime exceptions and build some useful tools utilising exception systems to help with the application development process.

CUSTOM EXCEPTIONS
=================

Custom built exceptions are a handy tool for both release and development versions of an application. They can be used in the following ways:

Custom Exceptions as Developer Warnings
---------------------------------------

We can use exceptions to remind us of not providing requisite information to a class or object. For example: We build a class as an interface to multiple vendor databases, let's call it Multiple Vendor Management (MVM) class. To connect to a particular database we perforce provide the instantiated MVM class with the vendor database type we are connecting to via a database_type method within the class. The MVM class also has a SQL_select method that can be called to return a recordset from our chosen vendor database. Within the SQL_select method is a switch (select case) statement which decides how to send the 'select request' to our chosen database type. If we fail to provide the vendor database type to our class and call the SQL_select method at runtime or a debug compilation, a default (case else) option within our switch (select case) statement throws a custom exception for developers. The custom exception will remind a developer during runtime testing that the requirements of the class have not been fulfilled during our coding. Below is an example of where our custom exception can be used:

Private Sub SQL_select(SQL As String)

dim exp_developer_1 as RuntimeException
exp_developer_1 = new RuntimeException
exp_developer_1.Message = "'database_type' property is not set."
exp_developer_1.ErrorNumber = -90000

dim rs As RecordSet

select case database_type

case MySQL_DATABASE
if Connect_mysql then
rs = mysql.SQLSelect(SQL)
end if // Connect_mysql

case ODBC_DATABASE
if Connect_odbc then
rs = odbcdb.SQLSelect(SQL)
end if // Connect_odbc

case REAL_DATABASE
if connect_rbdb then
// replace any instance of the word DISTINCT with UNIQUE to cater for RBDB syntax.
SQL = Replace_passed_regex_strings(SQL,"sDISTINCTs"," UNIQUE ")
rs = rbdb.SQLSelect(SQL)
end if // Connect_rbdb

case else
Raise exp_developer_1

end select

Exception err

d=New MessageDialog
if err = exp_developer_1 then
d.Set_Icon_Caution_Triangle
else
d.Set_Icon_Stop
end if

d.ActionButton.Caption = "OK"

#if TargetWin32 or TargetLinux Then
//ERR_MODULE is a constant that holds the module's/form's name + a full stop
d.Title = ERR_MODULE + "SQL_select"

if err = exp_db_error then
d.Message = d.ERROR_NUMBER_TEXT + cstr(exp_db_error.ErrorNumber) + _
EndOfLine + d.ERROR_DESCRIPTION_TEXT + exp_db_error.Message 'message
else

d.Message = d.ERROR_NUMBER_TEXT + cstr(err.ErrorNumber) + _
EndOfLine + d.ERROR_DESCRIPTION_TEXT + err.message 'message
end if
#else

//ERR_MODULE is a constant that holds the module's/form's name + a full stop
d.Message = d.ERROR_ROUTINE_TEXT + ERR_MODULE + "SQL_select" + EndOfLine if err = exp_db_error then
d.Message = d.Message + d.ERROR_NUMBER_TEXT +
cstr(exp_db_error.ErrorNumber) + _
EndOfLine + d.ERROR_DESCRIPTION_TEXT + exp_db_error.message 'message else

d.Message = d.Message + d.ERROR_NUMBER_TEXT + cstr(err.ErrorNumber) + _
EndOfLine + d.ERROR_DESCRIPTION_TEXT + err.message 'message
end if

No comments: