所以我在我的 Global.asax 页面中处理我的项目中的所有异常。例如,在一个随机页面上,我可以:
Protected Sub SomeFunction()
Try
'do something here
Catch ex As Exception
Throw ex
End Try
End Sub
然后异常冒泡到 Global.asax 页面,我在其中处理它并像这样登录到数据库:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ex As Exception = Server.GetLastError
Dim loggedExceptionID As Integer = 0
Try
loggedExceptionID = New BLL.ExceptionHandler().LogException(ex)
Response.Redirect("~/ErrorPage.aspx?ID=" & loggedExceptionID, False)
Catch loggingError As Exception
'what do I do here?
End Try
End Sub
BLL.ExceptionHandler().LogException(ex) 函数只是将错误详细信息写入数据库。
所以我的问题是在 Application_Error 方法中,尝试写入数据库时是否需要 try/catch 块?我想以防万一连接出现问题等 - 但这真的有必要吗?另外,如果出现错误,我会在 catch 块中做什么?那时我会发现记录错误的错误,这本身就令人困惑。