473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

ASP.NET Exception Handling

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
Apr 22 '06 #1
2 2649
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Brad Baker" wrote:
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

Apr 23 '06 #2
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.)

Apr 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.