【问题标题】:Try catch finally blocks.. do i still need them when handling errors in the global.asax?尝试 catch finally 块.. 处理 global.asax 中的错误时我还需要它们吗?
【发布时间】:2011-02-26 18:39:52
【问题描述】:

我在这个方法中通过我的 global.asax 处理错误:

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()

在我的代码中,我目前没有其他错误处理。我还应该插入 try、catch、finally 块吗?

谢谢。

【问题讨论】:

    标签: vb.net exception exception-handling try-catch


    【解决方案1】:

    绝对的。

    您应该始终在尽可能靠近源的位置处理异常。这使您可以做出适当的响应,例如重试失败的操作、关闭任何打开的资源、向用户提供反馈或编写比堆栈跟踪提供给您的更具体的日志信息(上下文等)

    然后,您可以(如果您愿意)再次抛出异常以处理全局“捕获所有”错误,但除非它是微不足道或意外的情况,否则您应该从源头检查潜在的异常。当您编写的代码可能相当合理地抛出异常(通常是文件 IO 等)但不一定表示 致命 错误时,尤其如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      相关资源
      最近更新 更多