views:

44

answers:

1

I am handling errors via my global.asax in this method:

Dim CurrentException As Exception
CurrentException = Server.GetLastError()
Dim LogFilePath As String = Server.MapPath("~/Error/" & DateTime.Now.ToString("dd-MM-yy.HH.mm") & ".txt")
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(LogFilePath)
sw.WriteLine(DateTime.Now.ToString)
sw.WriteLine(CurrentException.ToString())
sw.Close()

In my code I currently have no other error handling. Should I still insert try, catch, finally blocks?

Thanks.

+1  A: 

Absolutely.

You should always handle exceptions as close to the source as possible. This lets you respond appropriately, for example retrying a operation that failed, closing any open resources, giving feedback to the user, or writing more specific log information than the stack trace will give you (context etc.)

You can then (if you want to) throw the exception again for your global "catch all" error handling to deal with, but unless it is a trivial or unexpected case, you should be checking for potential exceptions at the source. This is especially true when the code you are writing might quite reasonably throw an exception (typically things like file IO etc.) but which do not necessarily indicate fatal errors.

xan