Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1219
  • Last Modified:

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?
0
Extraneus
Asked:
Extraneus
  • 5
  • 4
1 Solution
 
raterusCommented:
Try just doing this,

Try
   ...
Catch ex As Exception
   Throw
End Try
0
 
ExtraneusAuthor Commented:
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.
0
 
raterusCommented:
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)

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ExtraneusAuthor Commented:
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?


0
 
raterusCommented:
You shouldn't _have_ to do any of this.  When I programmed in .net 1.1, using a simple "Throw", threw the exception up the stack, and the errors always stated the exact line the exception was thrown on. (NOT the throw statement!)

I'm very surprised this isn't the case here, and seeing the lack of help out there on this issue, it makes me think only you and I are having it!

I'm also surprised that "Throw New Exception(ex.Message, ex)" isn't working for you.  I actually figured that out today, and just came in handy for me (so it's working for me).  The only other possibility is all your code above this statement is causing an addition error?  Have you tried just simply putting?

Catch ex as Exception
     Throw New Exception(ex.Message, ex)
End Try
0
 
ExtraneusAuthor Commented:
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.)
0
 
raterusCommented:
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)
0
 
ExtraneusAuthor Commented:
Excellent help.  Really appreciate raterus's time.
0
 
ExtraneusAuthor Commented:
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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now