ExpectedExceptionBaseAttribute Class
This is a base class for attributes that specify to expect an exception from a unit test.
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
System::Attribute
Microsoft.VisualStudio.TestTools.UnitTesting::ExpectedExceptionBaseAttribute
Microsoft.VisualStudio.TestTools.UnitTesting::ExpectedExceptionAttribute
| Name | Description | |
|---|---|---|
![]() | ExpectedExceptionBaseAttribute() | Initializes a new instance of the ExpectedExceptionBaseAttribute class. |
![]() | ExpectedExceptionBaseAttribute(String^) | Initializes a new instance of the ExpectedExceptionBaseAttribute class. |
| Name | Description | |
|---|---|---|
![]() | NoExceptionMessage | This API supports the product infrastructure and is not intended to be used directly from your code. |
![]() | TestContext | This API supports the product infrastructure and is not intended to be used directly from your code. |
![]() | TypeId | (Inherited from Attribute.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | (Inherited from Attribute.) |
![]() | Finalize() | (Inherited from Object.) |
![]() | GetHashCode() | (Inherited from Attribute.) |
![]() | GetType() | (Inherited from Object.) |
![]() | IsDefaultAttribute() | (Inherited from Attribute.) |
![]() | Match(Object^) | (Inherited from Attribute.) |
![]() | MemberwiseClone() | (Inherited from Object.) |
![]() | RethrowIfAssertException(Exception^) | Throws the exception again if it is an AssertFailedException or an AssertInconclusiveException. |
![]() | ToString() | (Inherited from Object.) |
![]() | Verify(Exception^) | This API supports the product infrastructure and is not intended to be used directly from your code. |
| Name | Description | |
|---|---|---|
![]() ![]() | _Attribute::GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | (Inherited from Attribute.) |
![]() ![]() | _Attribute::GetTypeInfo(UInt32, UInt32, IntPtr) | (Inherited from Attribute.) |
![]() ![]() | _Attribute::GetTypeInfoCount(UInt32) | (Inherited from Attribute.) |
![]() ![]() | _Attribute::Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | (Inherited from Attribute.) |
By implementing your own expected exception verification. you can specify additional information and requirements that the built-in methods of the ExpectedExceptionAttribute class cannot handle, such as the following:
Verifying the state of the exception.
Expecting more than one type of exception.
Displaying a custom message when a wrong type of exception is thrown.
Controlling the outcome of a negative test.
For more information about how to use attributes, see Extending Metadata Using Attributes.
Legacy Code Example
The following class contains the method to test.
using System;
namespace CSExample
{
public class DivisionClass
{
private int fraction;
public int Divide(int numerator, int denominator)
{
return numerator / denominator;
}
}
}
The following custom attribute class, ExpectedArithmeticException, derives from the ExpectedExceptionBaseAttribute class to verify an exception type and message. The ExpectedArithmeticException attribute verifies that a test method throws an exception of a type that is or derives from System.ArithmeticException. It also verifies that the exception message matches the specified exception message. The unit test will pass because it throws a DivideByZeroException, which derives from ArithmeticException, and the specified exception message matches the message of the exception that is thrown by the unit test.
using CSExample;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
public sealed class ExpectedArithmeticException : ExpectedExceptionBaseAttribute
{
private string exceptionMessage;
private string wrongExceptionMessage;
public string WrongExceptionMessage
{
get
{
return wrongExceptionMessage;
}
set
{
wrongExceptionMessage = value;
}
}
public ExpectedArithmeticException(string expectedExceptionMessage) : this(expectedExceptionMessage, "No exception was thrown.")
{
}
public ExpectedArithmeticException(string expectedExceptionMessage, string noExceptionMessage)
: base(noExceptionMessage)
{
exceptionMessage = expectedExceptionMessage;
WrongExceptionMessage = "The exception that was thrown does not derive from System.ArithmeticException.";
}
protected override void Verify(System.Exception exception)
{
Assert.IsNotNull(exception);
// Handle assertion exceptions from assertion failures in the test method, since we are not interested in verifying those
base.RethrowIfAssertException(exception);
Assert.IsInstanceOfType(exception, typeof(System.ArithmeticException), wrongExceptionMessage);
Assert.AreEqual(exceptionMessage, exception.Message, "Could not verify the exception message.");
}
}
[TestClass()]
public class DivisionClassTest
{
/* This test will pass because it thows a System.DivideByZeroException which derives from System.ArithmeticException. */
[TestMethod()]
[ExpectedArithmeticException("Attempted to divide by zero.", "An exception was expected, but no exception was thrown.", WrongExceptionMessage = "The wrong type of exception was thrown.")]
public void DivideTest()
{
DivisionClass target = new DivisionClass();
int numerator = 5;
int denominator = 0;
int actual;
actual = target.Divide(numerator, denominator);
}
}
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.





