开发者

Issue with Throw in asp.net

Below, why doesn't Throw maintain my originating line number?

If I run the DerivedPage, my log file lists the error as line 7.

Shouldn't it say line 4? It would make sense if I had Throw ex, but I don't.

Isn't Throw by itself just supposed to rethrow and bubble up the error?

If I remove the Try...Catch block entirely in DerivedPage, then my log file correctly lists 3 as the error开发者_Python百科 line, but I am not able to log any info in case of an error.

What can I do to maintain my DerivedPage, and still have my log keep the correct line number?

Public Class DerivedPage Inherits BasePage
 Page_Load(o,e)
   Try
      Dim a = 3 -"a"
   Catch ex As Exception
      log.Info(...)
      Throw
   End Try
End Class

base page:

Public Class BasePage
 Protected Overrides Sub OnError(e)
   MyBase.OnError(e)
   log.Error(Me.GetType(), Server.GetLastError)
 End Sub
End Class

EDIT: log.Error does output the InnerException if it exists. It does in this case. However, the stack trace for the InnerException doesn't contain a line number, just the Exception details.


When an exception is rethrown, the original exception details are stored in the InnerException property of the object. You should have all of your details there.


There is a method that you can implement that will give you the correct line number in your stack trace:

Rethrowing exceptions and preserving the full call stack trace - Fabrice's weblog

The code is in C#, but the code for the PreserveStackTrace method should be relatively easy to port over to VB.NET.


SOLUTION: Two solutions per Wrong line number on stack trace are throw a new exception with the current exception as the inner, or use a helper method.
I'm going to go with throwing a new exception.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜