Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ExpectedExceptionAttribute Class

 

Indicates that an exception is expected during test method execution. This class cannot be inherited.

Namespace:   Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

System.Object
  System.Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionBaseAttribute
      Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute

<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False,
	Inherited := True)>
Public NotInheritable Class ExpectedExceptionAttribute
	Inherits ExpectedExceptionBaseAttribute

NameDescription
System_CAPS_pubmethodExpectedExceptionAttribute(Type)

Initializes a new instance of the ExpectedExceptionAttribute class with an expected exception.

System_CAPS_pubmethodExpectedExceptionAttribute(Type, String)

Initializes a new instance of the ExpectedExceptionAttribute class with and expected exception type and a message that describes the exception.

NameDescription
System_CAPS_pubpropertyAllowDerivedTypes

System_CAPS_pubpropertyExceptionType

Gets the expected exception type.

System_CAPS_protpropertyNoExceptionMessage

This API supports the product infrastructure and is not intended to be used directly from your code. (Inherited from ExpectedExceptionBaseAttribute.)

System_CAPS_protpropertyTestContext

This API supports the product infrastructure and is not intended to be used directly from your code. (Inherited from ExpectedExceptionBaseAttribute.)

System_CAPS_pubpropertyTypeId

(Inherited from Attribute.)

NameDescription
System_CAPS_pubmethodEquals(Object)

(Inherited from Attribute.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Attribute.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_pubmethodIsDefaultAttribute()

(Inherited from Attribute.)

System_CAPS_pubmethodMatch(Object)

(Inherited from Attribute.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

System_CAPS_protmethodVerify(Exception)

This API supports the product infrastructure and is not intended to be used directly from your code. (Inherited from ExpectedExceptionBaseAttribute.)

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:

Public Class DivisionClass
   Public Function Divide(ByVal numerator As Integer, ByVal denominator As Integer) As Integer
      Return numerator \ denominator
   End Function
End Class

The following test method tests the Divide method of the DivisionClass object. It tests for the existence of a DivideByZeroException.

Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports MyVBProject

<TestClass()> _
Public Class DivisionClassTest
    <TestMethod()> _
    <ExpectedException(GetType(System.DivideByZeroException), "MyMessage")> _
    Public Sub DivideTest()
        Dim target As DivisionClass = New DivisionClass
        Dim numerator As Integer = 4
        Dim denominator As Integer = 0
        Dim actual As Integer
        actual = target.Divide(numerator, denominator)
    End Sub
End Class

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: