开发者

Try catch finally blocks.. do i still need them when handling errors in the global.asax?

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(CurrentExcept开发者_开发知识库ion.ToString())
sw.Close()

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

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜