Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-14
9
Medium Priority
?
1,211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 20281590
Try just doing this,

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

Author Comment

by:Extraneus
ID: 20282874
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
ID: 20283001
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Extraneus
ID: 20283184
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
 
LVL 33

Accepted Solution

by:
raterus earned 2000 total points
ID: 20283359
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
ID: 20283808
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
ID: 20284509
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
ID: 31409180
Excellent help.  Really appreciate raterus's time.
0
 

Author Comment

by:Extraneus
ID: 20292119
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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