2

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.

eych
  • 1,262
  • 2
  • 16
  • 37

3 Answers3

0

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.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • log4Net does print out the InnerException if it exists. And it does in this case. However, the InnerException stack trace does not contain the line number of the error. It just gives the error type. – eych Apr 14 '11 at 15:26
  • @eych: Have you tried updating your log statement to Log.Error(Me.GetType(), Server.GetLastError.InnerException) ? – Dillie-O Apr 14 '11 at 15:27
0

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

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.

Community
  • 1
  • 1
eych
  • 1,262
  • 2
  • 16
  • 37