CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 36
  1. #16
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    Debugging showed me that the problem was that mSessionData is null so therefore mSessionData.sLatestErr doesnt work. To tackle this I created a new instance of SessionData on each page, i.e.:
    Code:
    public SessionData mSessionData = new SessionData();
    I also gave sLatestError a test value. Now in my SessionData class I received a different error: StackOverFlowException! This is present here:

    Code:
     public ErrorPage errorPage = new ErrorPage();
    In other words the ErrorPage is stuck in some loop, presumably endlessly creating new instances of itself.I havent been able to figure out why since the ErrorPage seems fine to me but I have realized another problem! At the moment every page creates a new instance of ErrorPage. Doesnt this mean that every page automatically loads ErrorPage.aspx even if there is no error??

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

    Re: HttpException not handled

    That would suggest that Server.GetLastError() gave you back a null. You need to run it in debug mode and you'll have access to the original error or exception. You can put a breakpoint in the line of code where you 'GetLastError'. You can check through the call stack, you can check the local variables and you're are likely to have a better picture of what is happening. Have you checked the event log yet?

  3. #18
    Join Date
    Jan 2010
    Posts
    130

    Unhappy Re: HttpException not handled

    I managed to figure out why my ErrorPage didnt work! The ErrorPage was derived from a Master page which in turn directed one to the ErrorPage in the case of errors. When I tried loading a certain page containing errors it directed me to the ErrorPage which directed me to the Master page and then back to the ErrorPage (cas there too there were errors!) and so forth in a never ending loop! I now changed the ErrorPage to a single webform and it appears perfectly

    I still dont know why sLatestError returns null but I will follow your advice tomorrow morning and see if I get any further with that!

  4. #19
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    I am using Server.GetLastError() to determine the error that has been displayed. I have created a list which gives my own error messages for the most common errors in my app as follows:

    Code:
                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
                {
                    sMsgErr = "Ein Fehler ist aufgetreten.";
                }
    In my original code I tackle many more errors but I still keep receiving the last error message. Do you think it is possible that the exception that is returned with Server.GetLastError() is much longer than just the exception name (ex: "IndexOutOfRangeException") and that the corresponding error msg is therefore not being found? How would I tackle this apart from splitting ever error that comes in to see if it contains any of the following words (since that seems like a very long method)?

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

    Re: HttpException not handled

    Quote Originally Posted by stephsh
    In my original code I tackle many more errors but I still keep receiving the last error message. Do you think it is possible that the exception that is returned with Server.GetLastError() is much longer than just the exception name (ex: "IndexOutOfRangeException") and that the corresponding error msg is therefore not being found? How would I tackle this apart from splitting ever error that comes in to see if it contains any of the following words (since that seems like a very long method)?
    Reply With Quote
    Well you don't have to have a specific message for every possible type of error. Try to give specific message for errors that user can do something about or even if they can't then at least they get a message that is useful. For example an "IndexOutofRangeException" normally means a programming error so this is not useful at all. The user cannot do anything about it. The "UnauthorizedAccessException" is more useful as the user can speak to someone to grant them the right level of access if they need to. By the way why are you comparing against the exception as strings? If an exception was raised it has a type e.g. IndexOutOfRangeException. i don't understand why you are looking "IndexOutOfRangeException".

  6. #21
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    Good point, I will restrict the error messages to a couple of common ones such as "UnauthorizedAccessException" and "FileNotFoundException".

    The problem I am having is that Server.GetLastError() returns null! I tried saving the value of Server.GetLastError() as a session variable but null was still returned. I also tried handling the error in the Application_Error section of Global.asax but no luck with that either. Are there any other methods one can use to get the current error?

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

    Re: HttpException not handled

    Have you actually tried to run your application in debug mode? It will pinpoint exactly where the error is. If Server.GetLastError() returned null it suggest no error was detected or something is interefering with the way the error is handled or reported. If you would run your application in debug mode you will be able to find out exactly where the error is taking place and how it is being handled.

  8. #23
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    Yep, I have spent ages debugging but didnt get much out of it. Server.GetLastError() returned an empty string (since I declared the variable as below) and therefore my other methods returned null.

    Code:
            public string sLatestErr = String.Empty;

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

    Re: HttpException not handled

    Quote Originally Posted by stephsh View Post
    Yep, I have spent ages debugging but didnt get much out of it. Server.GetLastError() returned an empty string (since I declared the variable as below) and therefore my other methods returned null.

    Code:
            public string sLatestErr = String.Empty;
    Ok. Did you debug from the very beginning? Was there any error to begin with? If you call GetLastError when there was was no error (exception) you are supposed to get null.

  10. #25
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    Yes, I did debug it from the beginning. In the code below for example "e" brings up an error msg stating that the directory is not found. sLatestErr however only returns "", i.e. an empty string. When I mouse over "Server" the property "error" is null. I havent however managed to scroll over GetLastError to return it's properties.

    Code:
       public void CreatePDF()
            {
                doc = new Document(PageSize.A4, 80, 50, 30, 65);             
                string PDFpath = Server.MapPath("~/pdf/Beurteilungsbogen.pdf");
    
                try
                {
                    stream = new FileStream(PDFpath, FileMode.Create);
                    writer = PdfWriter.GetInstance(doc, stream);
                    doc.Open();
    
                    FormatPDF();
    
                    // First read the PDF before stamping it
                    PdfReader pdfReader = new PdfReader(Request.MapPath("~/pdf/Beurteilungsbogen.pdf"));
                    int pageNumber = 1;
                    Rectangle size = pdfReader.GetPageSizeWithRotation(pageNumber);
                    PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
                    pdfStamper.FormFlattening = true;
                    pdfStamper.Close();
            }
                catch (Exception e)
                {
                    SData.sLatestErr = Convert.ToString(Server.GetLastError());
                    string test = SData.sLatestErr; // Returns ""
                    Response.Redirect("Error.aspx", true);
                }

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

    Re: HttpException not handled

    Quote Originally Posted by stephsh View Post
    Yes, I did debug it from the beginning. In the code below for example "e" brings up an error msg stating that the directory is not found. sLatestErr however only returns "", i.e. an empty string. When I mouse over "Server" the property "error" is null. I havent however managed to scroll over GetLastError to return it's properties.
    Why do you need to call GetLastError when you have the details in the exception?

  12. #27
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    What I wanted to do is return the exception type. Using if statements I then wanted to see if the exception type is equal to an exception type listed in my array. If so then the corresponding error message should be displayed. If not then a standard error message can be displayed. That way the user can learn what type of exception it is but doesnt have to read through a long complicated error message. I thought that GetLastError returns the exception type?

    If this is not possible, then I will simply display the current error message using:

    Code:
     catch (Exception e)
                {
                    SData.sLatestErr = Convert.ToString(e.Message);
                    Response.Redirect("Error.aspx", true);
                }

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

    Re: HttpException not handled

    The best thing is to handle the exception at the point where it occurs and setup the different error messages there. The exception can tell you what type it is. Better still catch the specific exceptions you are prepared to handle...

    Code:
    try
    {
    }
    catch (FileNotFoundException) // Catch each specific exception that might be useful to handle separately. Eg. giving useful error message.
    {
       // Configure the correct error message here.
    }
    catch (IndexOutOfRangeException) // Catch each specific exception that might be useful to handle separately. Eg. giving useful error message.
    {
       // Configure the correct error message here.
    }
    catch (SqlException) // Catch each specific exception that might be useful to handle separately. Eg. giving useful error message.
    {
       // Configure the correct error message here.
    }
    catch (Exception)   // Use this only for general exceptions
    {
       // Configure the generic error message here.
    }
    Doing the look up against an array is an interesting thing to do but does not seem necessary here. If you can find a good enough reason to do that at least use an array of types and not an array of strings. But even as I'm typing I hear a loud voice in my head saying this not the best way to deal with the problem. Handle the exception where it occurs. All the error page should be concerned with doing is display a message. It doesn't have to be the one to decide which message to use. Do that decision at the point where the exceptions are caught.

  14. #29
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    My application is quite big so I have at least 8 try/catch blocks per page, often much more. Using all these catch statements would dramatically increase the code. I therefore want to handle the errors in a seperate class.

    How would one code this with an array of types? I am having trouble getting the actual error exception type as opposed to the exception message.

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

    Re: HttpException not handled

    You don't need to have exception handling at the page level. General exceptions should be handled globally anyway since the handling would be the same regardless of the page. I think I've mentioned this before that there are certain exceptions that do not need to be handled in several different places. I'm not sure what you've got in your pages but things like HttpException, UnauthorisedAccessException, SqlException you can handle in one place or a couple of places. You don't need to repeat the handling in every page. I realise your application is big. This is even more reason to re-design your exception handling otherwise it will become very difficult to maintain your application.

    Here's a possibility for storing the target exception types in an array.
    Code:
    private Type[] _arrayOfTypes = new Type[]
      { 
          typeof(IndexOutOfRangeException), 
          typeof(HttpException)
       };
    Last edited by nelo; February 10th, 2010 at 08:26 AM. Reason: Removed redundant braces...

Page 2 of 3 FirstFirst 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
  •  





Click Here to Expand Forum to Full Width

Featured