ExpectedExceptionAttribute Class
Indicates that an exception is expected during test method execution. This class cannot be inherited.
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 | |
|---|---|---|
![]() | ExpectedExceptionAttribute(Type^) | Initializes a new instance of the ExpectedExceptionAttribute class with an expected exception. |
![]() | ExpectedExceptionAttribute(Type^, String^) | Initializes a new instance of the ExpectedExceptionAttribute class with and expected exception type and a message that describes the exception. |
| Name | Description | |
|---|---|---|
![]() | AllowDerivedTypes | |
![]() | ExceptionType | Gets the expected exception type. |
![]() | NoExceptionMessage | This API supports the product infrastructure and is not intended to be used directly from your code. (Inherited from ExpectedExceptionBaseAttribute.) |
![]() | TestContext | This API supports the product infrastructure and is not intended to be used directly from your code. (Inherited from ExpectedExceptionBaseAttribute.) |
![]() | TypeId | (Inherited from Attribute.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | (Inherited from Attribute.) |
![]() | GetHashCode() | (Inherited from Attribute.) |
![]() | GetType() | (Inherited from Object.) |
![]() | IsDefaultAttribute() | (Inherited from Attribute.) |
![]() | Match(Object^) | (Inherited from Attribute.) |
![]() | ToString() | (Inherited from Object.) |
![]() | Verify(Exception^) | This API supports the product infrastructure and is not intended to be used directly from your code. (Inherited from ExpectedExceptionBaseAttribute.) |
| 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.) |
This attribute is used to test whether an expected exception is thrown. The test method will pass if the expected exception is thrown. The test will fail if the thrown exception inherits from the expected exception.
If a test has both an ExpectedException attribute and an Assert statement, either one can cause the test to fail. To determine whether the attribute or the statement caused the test to fail, double-click the test result to open the Results Details page for the test. For more information about test results, see . d1840991-0117-42a2-8ed1-1c11b57d08a8
This attribute can be specified on a method. There can be only one instance of this attribute on a method.
For more information about how to use attributes, see Extending Metadata Using Attributes.
Legacy Code Example
The following class contains the method to test:
The following test method tests the Divide method of the DivisionClass object. It tests for the existence of a DivideByZeroException.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.





