Table of Contents

Method Throws

Namespace
Xunit
Assembly
xunit.v3.assert.dll

Throws(Type, Action)

Verifies that the exact exception is thrown (and not a derived exception type).

public static Exception Throws(Type exceptionType, Action testCode)

Parameters

exceptionType Type

The type of the exception expected to be thrown

testCode Action

A delegate to the code to be tested

Returns

Exception

The exception that was thrown, when successful

Throws(Type, Func<object?>)

Verifies that the exact exception is thrown (and not a derived exception type). Generally used to test property accessors.

public static Exception Throws(Type exceptionType, Func<object?> testCode)

Parameters

exceptionType Type

The type of the exception expected to be thrown

testCode Func<object>

A delegate to the code to be tested

Returns

Exception

The exception that was thrown, when successful

Throws<T>(Action)

Verifies that the exact exception is thrown (and not a derived exception type).

public static T Throws<T>(Action testCode) where T : Exception

Parameters

testCode Action

A delegate to the code to be tested

Returns

T

The exception that was thrown, when successful

Type Parameters

T

The type of the exception expected to be thrown

Throws<T>(Func<object?>)

Verifies that the exact exception is thrown (and not a derived exception type). Generally used to test property accessors.

public static T Throws<T>(Func<object?> testCode) where T : Exception

Parameters

testCode Func<object>

A delegate to the code to be tested

Returns

T

The exception that was thrown, when successful

Type Parameters

T

The type of the exception expected to be thrown

Throws<T>(string?, Action)

Verifies that the exact exception is thrown (and not a derived exception type), where the exception derives from ArgumentException and has the given parameter name.

public static T Throws<T>(string? paramName, Action testCode) where T : ArgumentException

Parameters

paramName string

The parameter name that is expected to be in the exception

testCode Action

A delegate to the code to be tested

Returns

T

The exception that was thrown, when successful

Type Parameters

T

Throws<T>(string?, Func<object?>)

Verifies that the exact exception is thrown (and not a derived exception type), where the exception derives from ArgumentException and has the given parameter name.

public static T Throws<T>(string? paramName, Func<object?> testCode) where T : ArgumentException

Parameters

paramName string

The parameter name that is expected to be in the exception

testCode Func<object>

A delegate to the code to be tested

Returns

T

The exception that was thrown, when successful

Type Parameters

T