Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
API Reference for Testing Tools for Visual Studio ALM
ExpectedExceptionAttribute Class

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

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

Namespace:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
Visual Basic
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False, Inherited := True)> _
Public NotInheritable Class ExpectedExceptionAttribute _
    Inherits ExpectedExceptionBaseAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ExpectedExceptionAttribute : ExpectedExceptionBaseAttribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false, Inherited = true)]
public ref class ExpectedExceptionAttribute sealed : public ExpectedExceptionBaseAttribute
F#
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)>]
type ExpectedExceptionAttribute =  
    class
        inherit ExpectedExceptionBaseAttribute
    end
JScript
public final class ExpectedExceptionAttribute extends ExpectedExceptionBaseAttribute

The ExpectedExceptionAttribute type exposes the following members.

  NameDescription
Public methodExpectedExceptionAttribute(Type)Initializes a new instance of the ExpectedExceptionAttribute class with an expected exception.
Public methodExpectedExceptionAttribute(Type, String)Initializes a new instance of the ExpectedExceptionAttribute class with and expected exception type and a message that describes the exception.
Top
  NameDescription
Public propertyAllowDerivedTypes
Public propertyExceptionTypeGets the expected exception type.
Protected propertyNoExceptionMessageInfrastructure. (Inherited from ExpectedExceptionBaseAttribute.)
Protected propertyTestContextInfrastructure. (Inherited from ExpectedExceptionBaseAttribute.)
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top
  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodRethrowIfAssertExceptionThrows the exception again if it is an AssertFailedException or an AssertInconclusiveException. (Inherited from ExpectedExceptionBaseAttribute.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Protected methodVerifyInfrastructure. (Inherited from ExpectedExceptionBaseAttribute.)
Top
  NameDescription
Explicit interface implemetationPrivate method_Attribute..::.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute..::.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute..::.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute..::.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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 Test Results Reported.

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.

The following class contains the method to test:

C#
using System;

namespace MyCSNamespace
{
    public class DivisionClass
    {
        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}
Visual Basic
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.

C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyCSNamespace;

namespace MyCSTestProject
{
    [TestClass()]
    public class DivisionClassTest
    {
        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 4;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }
    }
}
Visual Basic
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.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker