Link to home
Start Free TrialLog in
Avatar of Extraneus
Extraneus

asked on

Server.GetLastError() doesn't show the actual line number of the error for Thrown exception

I'm using structured error handling in my ASP.NET app, so in Global.asax I have a

Sub Application_Error()
      Dim ex As Exception = Server.GetLastError()
      HttpContext.Current.Session("Exception") = ex
      Server.Transfer("~/Error.aspx")
End Sub

to redirect to an Error.aspx page which uses the session variable to handle the error.

This works fine for unhandled exceptions, but not if I explicitly Throw an exception.  If I use

Try
   ...
Catch ex As Exception
   ...
   Throw(ex)
End Try

my Error.aspx, which uses the StackTrance of the session variable created in Application_Error, shows the line number of the Throw statement, and all the functions called prior to the one which generated the error, not the line number from that function that actually generated the error.  I can write the exception to the console before the Throw statement and see the correct line number which generated the error, but how do I force this to show up in the GetLastError exception?
Avatar of raterus
raterus
Flag of United States of America image

Try just doing this,

Try
   ...
Catch ex As Exception
   Throw
End Try
Avatar of Extraneus
Extraneus

ASKER

I tried that, but it seems to result in the same problem.  I've taken to writing the StackTrace to a session variable before throwing the error, then in my Error.aspx appending this to the handled exception's StackTrace in those places where I use the Throw.  So now the new StackTrace message has the first two calls mixed up, since the first is the real last error, and the second is the Throw line, followed by the rest of the call stack.

Seems like there should be a better solution.
I actually did quite a bit of research before I posted that comment.  I've had the same problem, and I know I didn't have it back when I developed under asp.net 1.1.  I even tried a different computer and a new project, even C# to "fix" the problem, but nothing worked.

Also, let me point out that I don't think .GetLastError() is the culprit here.  Even if you took out your exception logging framework, you'd still see the issue.

"Throw" should keep the exception intact, not rewrite it from that point on, but it seems in this case it doesn't work like that.

Now here's my only "fix" at this point, I don't particularly like it since I've written "Throw" thousands of times in my code, but you might like it...

Throw New Exception(ex.Message, ex)

Thanks, raterus.  Unfortunately, no dice.  Maybe it's something I'm doing in my code, but I have a bunch of stuff going on in a Try block, then Catch ex As Exception, then I tried your suggestions like this:

Try
      ...
Catch ex As Exception
      Response.Write(ex.StackTrace)
      Throw New Exception(ex.Message, ex)     ' used to be Throw, before that Throw(ex)
End Try

with the Application_Error as I described above.  What happens is, the Error.aspx uses the Session("Exception") which Application_Error writes from Server.GetLastError, and the actual line which caused the error, from the Try block, never shows up in that StackTrace.  The last one in the stack is the Throw line, followed by the calls of other functions in the chain.  The Response.Write line above the Throw correctly shows the actual offending line, which never shows up either via Throw(ex), Throw, or Throw New Exception(ex.Message, ex).

I was hoping GetLastError might be the problem, and that there was a better way.  I tried writing to ex.StackTrace itself, but it's apparently ReadOnly.  Maybe I need my own Exception class which allows writing the StackTrace, so I can add to it before the Throw?


ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You know, I think this worked fine in 1.1, too.  As for the suggestion to comment out the other stuff in the Catch block, it doesn't change anything.  Now, if I Response.Write(ex.StackTrace) in the Catch block before the Throw, and compare that to Response.Write(ex.StackTrace) in the Application_Error after the Dim ex As Exception = Server.GetLastError, they're different.  The error message itself is fine, and explains the real error, but not the StackTrace, which doesn't show the offending line.  So it's nothing to do with my Error.aspx, just the exception before the throw, and the Server.GetLastError that gets caught in Application_Error.  (I think!  But of course, if I knew, I wouldn't be here.)
Hah, I got a solution (for me anyway)

99% of my catch statements look like this

Try
  ...
Catch ex as Exception
  Throw
Finally
  ...
End Try

I figured out you can get away with this (completely eliminate the catch block),

Try
  ...
Finally
 ...
End Try

-----------------

For you, there is a lot of official sounding information here,
http://blogs.msdn.com/jmstall/archive/2007/02/07/catch-rethrow.aspx

I also found a lot of information that says what we are experiencing is normal, and if you catch an exception, it is up to you to repackage it if you want to maintain the original stack track, e.g Throw new Exception(ex.Message, ex)
Excellent help.  Really appreciate raterus's time.
raterus, I added a section to Error.aspx which displays the ex.InnerException.StackTrace as well as ex.StackTrace, and your Throw New Exception(ex.Message, ex) solution works.  Now, the full call stack is in the ex.StackTrace, and the actual offending line from the Try block is in ex.InnerException.StackTrace.

Thanks for all the help!