Analytics


Google

Saturday, March 8, 2008

Error Handling in Applications

With Java and Microsoft .Net applications, the try ... catch statement is very powerful. There are many articles that says that the process is very costly. However, I found the following to be very effective in simplifying my application (will use vb.net in the example but can come up with similar example in Java if requested):

Sub process1()

try
...
Process2()
...
catch (ex as exception)
throw new Exception("[process01]" & ex.message)
end try

End Sub

Sub Process2
try
...
catch (ex as Exception)
throw new Exception("[process02]" & ex.Message())
End Try
End Sub

...

Public Sub Main(ByVal args() As String)
try
...
process1()
catch (ex As Exception)
Console.Writeline(ex.Message())
End Try


In the example above, if an error happens in Process2, it will be thrown upwards until it reaches the main subroutine. So when you see the error message, you know that the error was encountered in Process2. You will then see the error as

[process1][Process2]...


I found this makes it easier to zoom into the errors in my application. This concept can be used in Console applications, Web Applications and Windows Application. Just need to make sure that the message is displayed somewhere.

No comments: