Search code examples
asp.netvb.netexceptionlog4netthrow

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 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.


Solution

  • 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.