If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] HttpException not handled

I seem to be having problems loading my ErrorPage. I received the error message: "HttpException not handled". I then added a catch statement expecting a specified message to be written on my screen. Instead I again received the same message but this time my HttpException error mssage was highlighted. I would like to know the following:

1. I handled HttpExceptions so why do I still receive an error message instead of my own error message text appearing on the screen?
2. My error page is called ErrorPage.aspx and is not in any particular folder and has the same namespace as the other pages so why doesnt it work?
3. Could there be any errors in the method below which determines the error message type?

Re: HttpException not handled

Instead I again received the same message but this time my HttpException error mssage was highlighted.

I handled HttpExceptions so why do I still receive an error message instead of my own error message text appearing on the screen?

I don't understand what you are trying to say. Is your exception handler working? Do you actually catch the exception? If so how do you handle it? If you do catch the exception you can step through the code and see what is happening.

Re: HttpException not handled

Where are you trying to catch the application? Is it at the page level or application level? I would suggest handling the exception at the application level. You can do that in the 'global.ascx.cs'. This link has the details and it also includes re-directing to a custom error page.

Re: HttpException not handled

Brilliant, now my code looks much neater! I now deal with errors both on page level (try/catch blocks) and also in application level. I still however receive an error msg saying that my NullReferenceException isnt handled and highlighting that section in my ErrorPage!

Re: HttpException not handled

Ive discovered the error. I am calling up the method DisplayMessage() on the ErrorPage but this is only loading the C# code behind the ErrorPage.aspx and not the actual .aspx page containing my web controls. When debugging, lblErrorMsg returns value null! How do I ensure an instance of the actual page is also created? I cant properly fiddle around with the code because I call up an instance of the ErrorPage exactly 234 times!!

This is how I currently call up the ErrorPage, and it's appropriate error message, from other classes:

Re: HttpException not handled

I don't think that is the best to do it. You should redirect the ErrorPage and pass in any arguments through the query string. What you are doing is creating an instance of the class. The web page has to loaded and go through the full page life cycle.

Code:

// Define a class to hide the implementation details
// of the error handling. This way you will be able to change it
// in the future without changing a lot of code.
using System.Web;
public class ErrorUtility
{
public static void HandleError(string errorMessage)
{
Server.Transfer(String.Format("ErrorPage.aspx?ErrorMessage={0}", errorMessage));
}
}
// You will have to change the class to
// deal with the query string.
public class ErrorPage : Page
{
// This is one way of doing it.
protected void Page_PreRender(object sender, EventArgs e)
{
// Call the method that is already there to output the message.
DisplayErrorMsg(QueryString["ErrorMessage"]);
}
}

On another note...if the DisplayErrorMsg is used 234 times I think it would be a good idea to review how you are doing things. Unless you have 234 different web pages it shouldn't be called that many times...

Re: HttpException not handled

While waiting for a reply to my post I changed my code such that my try/catch blocks now only contain one Exception Handler (decreasing my amount of code enormously). Instead of calling the DisplayErrorMsg method in the catch block I now simply redirect to the ErrorPage. The ErrorPage's Page_Load method creates a new instance of a C# class called ErrorClass and calls it's DisplayErrorMsg method with the parameter "sCurrentErr". This method is very similar to yours with the exception that instead of passing the error as a query string (good idea!), I call the latest Server error. Your method seems better than mine and I will try to implement it but I am still interested in knowing why after my code STILL doesnt work (Error: error loading page)!!

Re: HttpException not handled

You have to give more details about the error you are experiencing now. There is likely to be an exception associated with it. You may want to run it in debug mode to get more information. You can also check the Event Log you may get ASP.NET error details there. Without the detail there's not much anyone can do with 'error loading page!'

Re: HttpException not handled

Another thing, how did you make Server.Transfer work in a static method? I have never been able to get either Response.Redirect or Server.Transfer to work in a static method...

Good question. The simple answer is I didn't. I just typed the code in codeguru. You are right. 'Server' is an instance property of the 'Page'. This would have worked...

Code:

// Define a class to hide the implementation details
// of the error handling. This way you will be able to change it
// in the future without changing a lot of code.
using System.Web;
public class ErrorUtility
{
public static void HandleError(string errorMessage)
{
HttpContext.Current.Server.Transfer(String.Format("ErrorPage.aspx?ErrorMessage={0}", errorMessage));
}
}

Re: HttpException not handled

my bad :P Basically I am loading a certain page (other than the ErrorPage) but due to errors on that page it redirects me to the ErrorPage. This then calls the Server.GetLastError method but this returns null... The rest of the methods can therefore not be executed properly...

Re: HttpException not handled

The problem with Server.GetLastError() is that it's value is reset to null everytime you leave a particular page. To tackle this I choose to save the value of Server.GetLastError() as a session variable called "sLatestErr". It however STILL has the value null!