Solved

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

Posted on 2007-11-14
9
1,191 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
Technology Partners: 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!

 

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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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