locked
Testing Custom Exception RRS feed

  • Question

  • The Framework Design Guidelines for Exceptions require an exception like:

    protected SomeException(SerializationInfo info, StreamingContext context);

    How should this exception be tested?

    Incidentally for any one wanting an example of unit testing the other three types of exception as required by the Framework Design Guidelines, please get the CommonData project from http://www.CodePlex.Com/CommonData


    Tuesday, November 18, 2008 10:50 AM

All replies

  • You can use the ExpectedException attribute. Generally, if a method is intended to throw an exception when we pass in certain parameters, we set the excepted exception with this attribute to the test method. The test framework will check if the correct exception is thrown as we excepted.


    Please mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Bill.Wang Monday, November 24, 2008 8:25 AM
    • Marked as answer by Bill.Wang Tuesday, November 25, 2008 3:14 AM
    • Unmarked as answer by Tatworth Monday, February 9, 2009 3:20 PM
    Thursday, November 20, 2008 5:02 AM
  • Whilst the post was helpful, it does not describe how to test

    protected SomeException(SerializationInfo info, StreamingContext context);


    How about an example to test the overload as required by the Framework Design Guidelines.

    • Edited by Tatworth Tuesday, February 10, 2009 12:46 PM
    Monday, February 9, 2009 3:23 PM
  • Hi Tatworth

    We implement this constructor on an exception to enable serialization of the exception.  This is important because an exception can carry across process boundaries.  For example, if it is thrown in a remote server and caught in the client and the mechanism the .Net Framework uses to accomplish this is serialization.

    Therefore you really need to test that your exception is serializable and deserializable.  That will exercise this contructor.  In your test create an instance of the exception, serialize it to an appropriate steam (I use binary), deserialize it and test all the properties were correctly re-initialized.  You will also need to mark your exception class as [Serializable] at class level.

    That should give your exception a good run for it's money.   My copy of the Framework Design Guidelines is at home so I can't check the exact reference but I hope this helps.

    Carlton
    • Proposed as answer by Carl355 Tuesday, February 10, 2009 7:45 AM
    Tuesday, February 10, 2009 6:13 AM
  • Thank you Carl for pointing me in the right direction. If you can provide a code sample, I would appreciate it.
    There is a "sort of" example at:
    I have followed the instructions in Framework Design Guidelines regarding a serialisable exception, but it does not indicate how to test.
    Tuesday, February 10, 2009 1:00 PM
  •    Here is a unit test derived from the MSDN article.

        #region " ExceptionTest7 "
        /// <summary>
        /// This example of the Exception with Serialization Info and StreamingContext parameters
        /// </summary>
        [Test]
        public void ExceptionTest7()
        {
          try
          {
            // This code forces a division by 0 and catches the resulting exception.
            try
            {
              var  zero = 0;
              var  ecks = 1 / zero;
            }
            catch( Exception ex )
            {
              // Create a new exception to throw again.
              var newExcept =
                    new DalGeneralException("Forced division by 0 and threw another exception.", ex);
              Console.WriteLine(
                  "Forced a division by 0, caught resulting exception and created a derived exception:\n" );
              Console.WriteLine( "HelpLink: {0}", newExcept.HelpLink );
              Console.WriteLine( "Source:   {0}", newExcept.Source );
              // This FileStream is used for the serialization.
              var stream = new FileStream( "NewException.dat", FileMode.Create );
              try
              {
                // Serialize the derived exception.
                var formatter = new SoapFormatter( null, new StreamingContext(StreamingContextStates.File ) );
                formatter.Serialize( stream, newExcept );
                stream.Position = 0;  // Rewind the stream and deserialize the exception.
                var deserExcept = (DalGeneralException)formatter.Deserialize( stream );
                Console.WriteLine(
                    "\nSerialized the exception, and then deserialized the resulting stream " +
                    "into a \nnew exception. The deserialization changed the case of certain properties:\n" );
                throw deserExcept;  // Throw the deserialized exception again.
              }
              catch( SerializationException se )
              {
                  Console.WriteLine( "Failed to serialize: {0}", se.ToString( ) );
              }
              finally
              {
                  stream.Close( );
              }
            }
          }
          catch( Exception ex )
          {
            Console.WriteLine( "HelpLink: {0}", ex.HelpLink );
            Console.WriteLine( "Source:   {0}", ex.Source );
            Console.WriteLine( ex.ToString( ) );
          }
        }
        #endregion
    Tuesday, February 10, 2009 1:55 PM