[RESOLVED] HttpException not handled
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: [RESOLVED] HttpException not handled

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [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?

    Code:
     public void DisplayErrorMsg()
            {
                // In case of error redirect to error page
                for (int i = 0; i < listError.Count; i++)
                {
                    if (listError[i] == currentError)
                    {
                        try
                        {
                            Response.Redirect("ErrorPage.aspx", true);  //ORIGINAL ERROR
                        }
                        catch (HttpException)
                        {
                            Response.Write("Fehlerseite wurde nicht gefunden."); //LATER ERROR HERE
                        }
                        catch (Exception)
                        {
                            Response.Write("Fehlerseite enthalt Fehler");
                        }
                    }
                }
    
                // Determine error message
                if (currentError == "401")
                {
                    lblErrorCode.Text = "Fehlercode: 401";
                    lblErrorMsg.Text = "Zugang zur Seite nicht erlaubt.";
                    lblErrorCode.Visible = true;
                    lblErrorMsg.Visible = true;
                }
                else if (currentError == "404")
                {
                    lblErrorCode.Text = "Fehlercode: 404";
                    lblErrorMsg.Text = "Seite nicht gefunden.";
                    lblErrorCode.Visible = true;
                    lblErrorMsg.Visible = true;
                }
    
                (ETC)
    
                else
                {
                    lblErrorCode.Text = "Ein Fehler ist aufgetreten.";
                    lblErrorCode.Visible = true;
                    lblErrorMsg.Visible = false;
                }
            }

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: HttpException not handled

    Hi there,

    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.

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    The problem is that I dont seem to be able to catch the HttpException...

    I discovered that my ErrorPage doesnt load because the NullReferenceException isnt caught. I however did handle it in my code....

    Code:
      listError.Add("NullReferenceException");
    (...)
                else if (currentError == "NullReferenceException")
                {
                    lblErrorCode.Text = "Fehler: NullReferenceException";
                    lblErrorMsg.Text = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
                    lblErrorCode.Visible = true;
                    lblErrorMsg.Visible = true;
                }
                else
                {
                    lblErrorCode.Text = "Fehler!";
                    lblErrorMsg.Text = "Exception";
                    lblErrorCode.Visible = true;
                    lblErrorMsg.Visible = true;
                }
    The problem seems to be that some of my exception are not caught even though I tried to handle them.

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    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!

    Code:
        public partial class ErrorPage : System.Web.UI.Page
        {
            /// <summary>
            /// Redirects to ErrorPage and provides corresponding error msg.
            /// </summary>
    
            protected void Page_Load(object sender, EventArgs e)
            {
                
            }
    
            #region Display Error Msg
    
            public void DisplayErrorMsg(string currentError) 
            {
                // Determine Error Message
                if (currentError == "401")
                {
                    lblErrorMsg.Text = "Zugang zur Seite nicht erlaubt.";
                }
                else if (currentError == "404")
                {
                    lblErrorMsg.Text = "Seite nicht gefunden.";
                }
                else if (currentError == "408") 
                {
                    lblErrorMsg.Text = "Sitzungsperiode fertig.";
                }
                else if (currentError == "HttpException")
                {
                    lblErrorMsg.Text = "Zugang zur Seite nicht erlaubt.";
                }
                else if (currentError == "UnauthorizedAccessException")
                {
                    lblErrorMsg.Text = "Zugang nicht erlaubt.";
                }
                else if (currentError == "IndexOutOfRangeException")
                {
                    lblErrorMsg.Text = "Die Angegebene ID existiert nicht.";
                }
                else if (currentError == "NullReferenceException") // PROBLEM HERE!!!
                {
                    lblErrorMsg.Text = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
                    //lblErrorMsg.Text = "Ein nicht vorhandenes Objekt wird (zu) verweisen.";
                }
                else if (currentError == "FileNotFoundException")
                {
                    lblErrorMsg.Text = "Die Datei wurde nicht gefunden.";
                }
                else if (currentError == "IOException")
                {
                    lblErrorMsg.Text = "Ein E/A-Fehler ist auftritten.";
                }
                else if (currentError == "SOAPException")
                {
                    lblErrorMsg.Text = "Ein Fehler ist bei die Aufruf einer XML-Webdienstmethode über SOAP aufgetritten.";
                }
                else if (currentError == "HttpException")
                {
                    lblErrorMsg.Text = "Ein Fehler ist bei die Aufruf einer Seite aufgetritten.";
                }
                else
                {
                    lblErrorMsg.Text = "Ein Fehler ist aufgetreten.";
                    lblErrorCode.Visible = false;
                }
    
                // Set error code and msg
                lblErrorCode.Text = "Fehler: " + currentError;
                lblErrorCode.Visible = true;
                lblErrorMsg.Visible = true;
    
                try
                {
                    // Redirect to error page:
                    Response.Redirect("ErrorPage.aspx", true);
                }
                catch (Exception)
                {
                    Response.Write("Ein Fehler ist bei die bei Aufruf der Fehlerseite aufgetritten.");
                }
            }

  6. #6
    Join Date
    Jan 2010
    Posts
    130

    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:

    Code:
     Azubiportal.ErrorPage errorPage = new ErrorPage();
    errorPage.DisplayErrorMsg("IndexOutOfRangeException");

  7. #7
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

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

  8. #8
    Join Date
    Jan 2010
    Posts
    130

    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)!!

    This is what my ErrorPage looks like:
    Code:
     public ErrorClass errorClass = new ErrorClass();
            public string sCurrentErr;
            public string sMsgErr;
    
            public void Page_Load(object sender, EventArgs e)
            {
                // Return last error
                sCurrentErr = Convert.ToString(Server.GetLastError());
                // Determine error msg
                sMsgErr = errorClass.GetErrorMsg(sCurrentErr);
                // Display error msg on .aspx page
                DisplayErrorMsg();
            }
    
            protected void DisplayErrorMsg()
            {
                // Set error code and msg
                lblErrorCode.Text = "Fehler: " + sCurrentErr;
                lblErrorMsg.Text = sMsgErr;
                lblErrorCode.Visible = true;
                lblErrorMsg.Visible = true;
    
            }
    And this is what my ErrorClass class looks like:
    Code:
    public class ErrorClass
        {
            /// <summary>
            /// Provides corresponding error msg
            /// to be used in ErrorPage
            /// </summary>
    
            private string sMsgErr;
    
            public ErrorClass()
            {
            }
    
            #region Get Error Msg
    
            public string GetErrorMsg(string currentError)
            {
                // Determine Error Message
                if (currentError == "401")
                {
                    sMsgErr = "Zugang zur Seite nicht erlaubt.";
                }
                else if (currentError == "404")
                {
                    sMsgErr = "Seite nicht gefunden.";
                }
                else if (currentError == "408")
                {
                    sMsgErr = "Sitzungsperiode fertig.";
                }
                else if (currentError == "HttpException")
                {
                    sMsgErr = "Zugang zur Seite nicht erlaubt.";
                }
                else if (currentError == "UnauthorizedAccessException")
                {
                    sMsgErr = "Zugang nicht erlaubt.";
                }
                else if (currentError == "IndexOutOfRangeException")
                {
                    sMsgErr = "Die Angegebene ID existiert nicht.";
                }
                else if (currentError == "NullReferenceException")
                {
                    sMsgErr = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
                    //errorMsg = "Ein nicht vorhandenes Objekt wird verweisen.";
                }
                else if (currentError == "FileNotFoundException")
                {
                    sMsgErr = "Die Datei wurde nicht gefunden.";
                }
                else if (currentError == "IOException")
                {
                    sMsgErr = "Ein E/A-Fehler ist auftritten.";
                }
                else if (currentError == "SOAPException")
                {
                    sMsgErr = "Ein Fehler ist bei die Aufruf einer XML-Webdienstmethode über SOAP aufgetritten.";
                }
                else if (currentError == "HttpException")
                {
                    sMsgErr = "Ein Fehler ist bei die Aufruf einer Seite aufgetritten.";
                }
                else
                {
                    sMsgErr = "Ein Fehler ist aufgetreten.";
                }
    
                return sMsgErr;
            }
    
            #endregion

  9. #9
    Join Date
    Jan 2010
    Posts
    130

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

  10. #10
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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!'

  11. #11
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: HttpException not handled

    Quote Originally Posted by stephsh View Post
    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));
       }
    }
    Here I'm using the Server property of the current http context.

  12. #12
    Join Date
    Jan 2010
    Posts
    130

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

  13. #13
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    I tried your method but when I pass the data as query string I receive the error that there are line breaks in the actual exception string! Here is an example:

    Code:
            protected void GetTrainee()
            {
                try
                {
                    // Try accessing the connection class to get the ID of the current trainee
                    trainee = WebServiceConnection.webService.getTrainee(Azubiportal.system.BaseWebPage.selectedTraineeID);
                }
                catch (Exception excep)
                {
                    Response.Redirect("ErrorPage.aspx?qstr=" + excep); 
    
                    // Method 2: 
                    // string temp = Convert.ToString(excep);
                    // Response.Redirect("ErrorPage.aspx?qstr=" + temp); 
    
                    // Method 3: excep.Message instead of temp
                }
            }

  14. #14
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: HttpException not handled

    Quote Originally Posted by stephsh View Post
    I tried your method but when I pass the data as query string I receive the error that there are line breaks in the actual exception string!
    Good point. I hadn't thought about that. You will probably have to resort to calling GetLastError within the error page or storing the message in a session variable.

  15. #15
    Join Date
    Jan 2010
    Posts
    130

    Question 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!

    This is what my session data looks like:
    Code:
        public class SessionData
        {
            /// <summary>
            /// SessionData object contains general information
            /// with regards to the user and the current status
            /// that can be used throughout application.
            /// </summary>
    
            #region Variables
    
            public Configuration mConfiguration;
            public BerechtTransactionHandler mBerechtTransactionHandler;
    
            public string sSessionID;
            //public string sUserType;
            public string sAdvisorName;
            public string sAdvisorEmail;
            public DateTime dtCurrentDate;
            public string sLatestErr; //= "";
    
            Azubiportal.ErrorPage errorPage = new Azubiportal.ErrorPage();
    
            #endregion
    
            public SessionData()
            {
            }
    
            public void setConfiguration()
            {
                try
                {
                    dtCurrentDate = DateTime.Now;
                    sSessionID = mBerechtTransactionHandler.mSessionID;
                    sAdvisorName = mConfiguration.msBerechtUserName;
                    sAdvisorEmail = mConfiguration.msEMailSender;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
    And this is an example of how Ive used the session variable sLatestErr:

    Code:
     protected void GetTrainee()
            {
                try
                {
                    // Try accessing the connection class to get the ID of the current trainee
                    trainee = WebServiceConnection.webService.getTrainee(Azubiportal.system.BaseWebPage.selectedTraineeID); 
                }
                catch (Exception)
                {
                    mSessionData.sLatestErr = Convert.ToString(Server.GetLastError());
                    Response.Redirect("ErrorPage.aspx", true);
                }
            }

Page 1 of 3 123 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center