ASP.NET Exception Handling

B

Brad Baker

I'm trying to create a custom ASP.NET custom error page however I am having
some problems. Here is what I have done:



1) I've created a page called default.aspx with the following code to
stimulate an error:



<script language="c#" runat="server">

public void Page_Load(object sender, EventArgs e) {



throw new Exception ("Test Exception");

}

</script>





2) I've edited my web.config file custom errors as follows:



<customErrors

mode="On"

defaultRedirect="errorreport.aspx"

/>





3) My errorreport.aspx contains the following code:



<script language="c#" runat="server">

public void Page_Load(object sender, EventArgs e) {



HttpContext ctx = HttpContext.Current;



Exception ex = Server.GetLastError().GetBaseException();



string errorInfo =

"<br>Offending URL: " + ctx.Request.Url.ToString () +

"<br>Source: " + ex.Source +

"<br>Message: " + ex.Message +

"<br>Stack trace: " + ex.StackTrace;



Response.Write (errorInfo);



}

</script>





When I access default.aspx, I get redirected to asperrorreport.aspx as
expected but the following error is generated on the errorreport.aspx page:



Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Line 12: Exception ex = Server.GetLastError().GetBaseException();



I believe this is because the "ex" variable is null. What I don't understand
is why it's null? What am I missing?



Thanks,

Brad
 
G

Guest

Put this code in the Application_Error event handler in your global.asax
Something like so:


// in global.asax:
protected void Application_Error(Object sender, EventArgs e)
{
// logging code line follows
ExceptionHandler.LogException exc = new ExceptionHandler.LogException();
Exception ex = Server.GetLastError().GetBaseException();
// pass ex to your logging or notification utility, write to event log, or
whatever.
}

And here is something you might find even more useful:

http://www.eggheadcafe.com/articles/20030816.asp

Peter
 
S

Steven Cheng[MSFT]

Hi Brad,

I think Peter's suggestion is reasonable. As for the Server.GetLastError,
it actually retrieve the error info from the current HttpContext, and if
you just let the runtime to forward user to the custom error page, it send
a new request to the page(maybe response.Redirect which end the original
HttpContext, that makes the custom error page fail to get the unhandled
exception. To resolve this problem, you can use the Application_Error event
handler to hook the unhandled exception and use Server.Transfer to forward
the current context to your custom error page. e.g:

===========================
protected void Application_Error(Object sender, EventArgs e)
{

//Get the exception through Server.GetLastError and log it,

Server.Transfer("MyCustomErrorpage.aspx");

}
==========================


Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! 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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top