Aug 13, 2007

Design Exception Handler Pattern

When we write application we should handle exception for block Application Exception when it occurs, our application may crash.

.Net developer regular design exception as this.

Try

‘statement for operation

‘statement for operation

‘e.g’

Dim CN as new Oledb.OledbConnection(strConnectionString)

CN.Open()

‘other statement for operation

Catch ex as Exception

‘statement for handler exception

‘e.g.

ModuleLogError.WriteLog(ex)

Finally

‘This statement will execute even code have exception or not

CN.Close

End try

This rule will help for design better structure of handle exception.

1. Check exception may occur before Exception

e.g. DivideByZeroException,ArithmeticException …

This below is a sample code

Try

‘’’

Catch(Ex as DivideByZeroException)

‘’’

Catch(Ex as ArithmeticException)

‘’’

Catch(Ex as Exception)

‘’’

End Try

2. Keep module detail to have occur exception

In a moudule we handler exception we shuld send information about module have occure exception e.g. Modulename,section of section

Dim strModuleName as string= “DAC.Customer” ‘This variable outside method because we shared it multiple method

Private Sub SaveData()

Dim strMethodName as string=”SaveData”

Dim strSection as string = “Begin Module”

Try

strSection = “Operation 1”

‘statement for operation1

strSection = “Operation 2”

‘statement for operation2

‘other statement for operation

Catch ex as Exception

ModuleLogError.WriteLog(ex,strModuleName,strMethodName,strSection,)

Finally

‘This statement will execute even code have exception or not

CN.Close

End try

End Sub

3. When we loop operation loop should in exception

e.g.

if your code is look like this:

dim I as integer

for I = 1 to 100

Try

‘statement

Catch ex as Exception

‘statement for handler

Finally

‘statement for release unsafe resource and other benefits

End try

next

It should refractor your code as below:

Try

Dim I as integer

For I = 1 to 100

‘statement

Next

Catch ex as Exception

‘statement for handler

Finally

‘statement for release unsafe resource and other benefits

End try

It is have high performance than before.

4. Avoid use exception in case we can handle it by operation checking

e.g.

if(Num1 = 0) then

MessageBox.show(“Value cannot be 0”)

Exit sub

end if

dim dblResult = Num2 / Num1

instead

try

Dim dblResult = Num2 / Num1

Catch ex as DivideByZeroException{

MessageBox.Show(“Value cannot be 0”)

End Try

Because exception is expensive, we should avoid in a case we can handle it

5.When you application design for contact with another layer (some dll ,e.g. BC layer, DAC layer) you may not throw exception, but should keep exception for client to handle it.

e.g

This is a DAC.

Namespace DAC

Public Class Customer

Dim currentException as ApplicationException

Public Sub AddData()

Try

‘’’ Statement

Catch Ex as Exception

Me.CurrentException = Ex

End Try

End Sub

End Class

End NameSpace

And this is a client code.

Dim cCustomer as New DAC.Customer

cCustomer.AddData

if(Not isnothing(cCustomer.currentException ))

‘code for handler exception of dac

‘e.g.

‘MessageBox.Show(ex.Tostring) ‘ for winform

‘Me.lblShowError.InnerText = ex.ToString ‘for web form

‘code for write log error

End if

6. In development phase you should display detail about exception instead hardcode wording for user

e.g.

Try

‘Code for Add data

Catch Ex as Exception

MessageBox.show(ex.toString)

Instead

MessageBox.show(“Error:Please enter againg”)

End Try

This will help you easy to find the problem, and then you pass development phase you may change wording to friendly word for user. Or you may send both error message and friendly word to user , but you should care about security issue.

7. Desing your own exception

If you wish to design you exception. It is easy to do, you just create a new class and inherit from ApplicationException

No comments: