### ExpectedExceptionAttribute (NUnit 2.0 / 2.2.4)

This is the way to specify that the execution of a test will throw an exception. This attribute has two constructors. The first (NUnit 2.0) takes a Type representing the exact type of the expected exception. The second (NUnit 2.2.4) takes the full name of the expected exception type as a string

In either case, the runner will execute the test, which passes only if it throws the specified exception. If it throws a different exception then the test will fail. This is true even if the thrown exception inherits from the expected exception.

#### Example:

C#
VB
C++

    namespace NUnit.Tests
    {
      using System;
      using NUnit.Framework;

      [TestFixture]
      public class SuccessTests
      {
        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void ExpectAnExceptionByType()
        { /* ... */ }

        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void ExpectAnExceptionByName()
        { /* ... */ }
      }
    }

    Imports System
    Imports Nunit.Framework

    Namespace Nunit.Tests

      <TestFixture()> Public Class SuccessTests
        <Test(), ExpectedException(GetType(Exception))>
          Public Sub ExpectAnExceptionByType()
        ' ...
        End Sub

      <TestFixture()> Public Class SuccessTests
        <Test(), ExpectedException("System.Exception")>
          Public Sub ExpectAnExceptionByName()
        ' ...
        End Sub
      End Class
    End Namespace

    #using <Nunit.Framework.dll>
    using namespace System;
    using namespace NUnit::Framework;

    namespace NUnitTests
    {
      [TestFixture]
      public __gc class SuccessTests
      {
        [Test]
        [ExpectedException(__typeof(InvalidOperationException))]
        void ExpectAnExceptionByType();

        [Test]
        [ExpectedException(S"SystemInvalidOperationException")]
        void ExpectAnExceptionByName();
      };
    }

    #include "cppsample.h"

    namespace NUnitTests {
      // ...
    }