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

Thread: [RESOLVED] HttpException not handled

  1. #31
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    Interesting, I have never seen that type of structure before. Could you explain it please?

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

    Re: HttpException not handled

    Code:
    private Type[] _arrayOfTypes = new Type[]
      { 
          typeof(IndexOutOfRangeException), 
          typeof(HttpException)
       };
    Type is just a class in the .NET framework...

    What's happening here is that I'm using the collection initializer construct from C#3.0/.NET 3.5. It enables me to create the array with the elements I need in a single statement.

    The verbose alternative would have been something like this...

    Code:
    Type[] _arrayOfTypes = new Type[2];
    _arrayOfTypes[0] = typeof(IndexOutOfRangeException);
    _arrayOfTypes[1] = typeof(HttpException);
    Well in terms of lines of code it is pretty much the same. I just like to use the new language features when I have the opportunity.

    Edit: I would like to put a disclaimer here to say that I'm aware that C#3.0 isn't exactly new...
    Last edited by nelo; February 10th, 2010 at 08:26 AM.

  3. #33
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    That makes more sense to me, thanks! I rewrote my ErrorClass using the code you gave me and I took out the error msgs which dont really concern the user. The question is how do I retrieve the error message type so that I can check it against the array?

    Code:
     // Create an array of types
            protected Type[] _arrayOfTypes = new Type[]
            {
                typeof(HttpException),
                typeof(UnauthorizedAccessException),
                typeof(FileNotFoundException),
                typeof(IOException),
                typeof(IndexOutOfRangeException),
                typeof(NullReferenceException)
            };
    
            // This is a different way for doing:
            #region Old method
            //protected void GetArray()
            //{
            //    _arrayOfTypes[0] = typeof(IndexOutOfRangeException);
            //    _arrayOfTypes[1] = typeof(HttpException);
            //}
    
            // where
            //protected Type[] _arrayOfTypes = new Type[2];
            #endregion
    
            public string GetErrorMsg(Type sError)
            {
                if (sError == _arrayOfTypes[0])
                {
                    sMsgErr = "Zugang zur Seite nicht erlaubt.";
                }
                else if (sError == _arrayOfTypes[1])
                {
                    sMsgErr = "Zugang nicht erlaubt.";
                }
                else if (sError == _arrayOfTypes[2])
                {
                    sMsgErr = "Die Datei wurde nicht gefunden.";
                }
                else if (sError == _arrayOfTypes[3])
                {
                    sMsgErr = "Ein E/A-Fehler ist auftritten.";
                }
                else if (sError == _arrayOfTypes[4])
                {
                    sMsgErr = "Die Angegebene ID existiert nicht.";
                }
                else
                {
                    sMsgErr = "Ein Fehler ist aufgetreten.";
                }
    
                return sMsgErr;
            }

  4. #34
    Join Date
    Jan 2010
    Posts
    130

    Re: HttpException not handled

    Sorry I meant:

    Code:
     // Create an array of types
            protected Type[] _arrayOfTypes = new Type[]
            {
                typeof(HttpException),
                typeof(UnauthorizedAccessException),
                typeof(FileNotFoundException),
                typeof(IOException),
                typeof(IndexOutOfRangeException)
            };
    
            public string GetErrorMsg(Type sError)
            {
                if (sError == _arrayOfTypes[0])
                {
                    sMsgErr = "Zugang zur Seite nicht erlaubt.";
                }
                else if (sError == _arrayOfTypes[1])
                {
                    sMsgErr = "Zugang nicht erlaubt.";
                }
                else if (sError == _arrayOfTypes[2])
                {
                    sMsgErr = "Die Datei wurde nicht gefunden.";
                }
                else if (sError == _arrayOfTypes[3])
                {
                    sMsgErr = "Ein E/A-Fehler ist auftritten.";
                }
                else if (sError == _arrayOfTypes[4])
                {
                    sMsgErr = "Die Angegebene ID existiert nicht.";
                }
                else if (sError == _arrayOfTypes[5])
                {
                    sMsgErr = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
                    //errorMsg = "Ein nicht vorhandenes Objekt wird verweisen.";
                }
                else
                {
                    sMsgErr = "Ein Fehler ist aufgetreten.";
                }
    
                return sMsgErr;
            }

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

    Re: HttpException not handled

    Quote Originally Posted by stepsh
    The question is how do I retrieve the error message type so that I can check it against the array?
    Code:
    try
    {
    }
    catch (Exception e)
    {
       ErroHandler.GetErrorMsg(e.GetType());
    }
    Every object in .NET framework implements a GetType method which should give you the type.

  6. #36
    Join Date
    Jan 2010
    Posts
    130

    Talking Re: HttpException not handled

    Thanks I can now display the type of exception! I havent been able to find an exception that fits my type yet but Im sure it works

Page 3 of 3 FirstFirst 123

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