Error-handling

L

Leo R

Hi all,

If an error occurs in an aspx-page, you can configure the web.config in a
way that the user automatically navigates to a certain page (e.g.
error.aspx).

My question: is it possible to catch the stacktrace of the error in
error.aspx so it can be logged? The error has been catched somewhere in
order to perform the reroute to error.aspx. So where are the error-details?

Leo R.
 
K

Kevin Spencer

The Error information is contained in the Exception that is thrown, which is
an object. Here is a sample of an Error Handler that I use frequently. The
reference to the "LogError()" method would be whatever you want to do with
your Exception data:

public static void HandleError(Exception e)
{
try
{
StringBuilder strMessage = new StringBuilder(e.Message);
if (e.InnerException != null)
strMessage.Append(Environment.NewLine + "Inner Exception: " +
e.InnerException.Message);
strMessage.Append(Environment.NewLine + "StackTrace: " + Environment.NewLine
+ e.StackTrace);
LogError(strMessage.ToString());
}
catch {}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
L

Leo R

Kevin,

Thanks for your reply. I understand how to catch an error. What I mean is:

An error is thrown in a certain aspx-page; let's say page1.aspx. As a result
of the Web.config seting for errors the user is redirected to error.aspx
because of the (unhandled) error in page1.aspx. I want to process the
stacktrace of the error that was thrown in the page1.aspx or any other page
in my webapplication in the page 'error.aspx'.

The reason why is that I don't want to put try catch blocks in all my
aspx-pages. Is that possible?

Leo R.
 
J

Jacob Yang [MSFT]

Hi Leo,

Based on my research and experience, I don't think that what you want to do
is possible. The reasons is "...Once the redirect is done, your error
information is no longer available on the redirected page...". Please refer
to the following article for the detailed information.

Web Application Error Handling in ASP.NET
http://www.15seconds.com/issue/030102.htm

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
K

Kevin Spencer

If you use the code I gave you and then throw the exception (in your catch
block) afterwards it will work as you wish.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
J

Jacob Yang [MSFT]

Hi Leo,

Firstly I want to thank Kevin for his great help in this issue.

If you want to do something before error processing goes to the
customErrors setup in the web.config file, you should either hook the the
Page_Error or override the OnError sub. For example,

Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error

End Sub

Or you can use this one:

Protected Overrides Sub OnError(ByVal e As System.EventArgs)
End Sub

For more info, please refer to the following Knowledge base article which I
have mentioned before

Web Application Error Handling in ASP.NET
http://www.15seconds.com/issue/030102.htm

I hope it helps.

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top