Solved

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

Posted on 2007-11-14
9
1,162 Views
Last Modified: 2010-08-05
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
Comment
Question by:Extraneus
  • 5
  • 4
9 Comments
 
LVL 33

Expert Comment

by:raterus
Comment Utility
Try just doing this,

Try
   ...
Catch ex As Exception
   Throw
End Try
0
 

Author Comment

by:Extraneus
Comment Utility
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
 
LVL 33

Expert Comment

by:raterus
Comment Utility
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
 

Author Comment

by:Extraneus
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
Comment Utility
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
 

Author Comment

by:Extraneus
Comment Utility
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
 
LVL 33

Expert Comment

by:raterus
Comment Utility
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
 

Author Closing Comment

by:Extraneus
Comment Utility
Excellent help.  Really appreciate raterus's time.
0
 

Author Comment

by:Extraneus
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now