Assert.Throws Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Throws<TException>(Func<Object>, Func<Exception,String>, String) |
Asserts that the delegate |
| Throws<TException>(Func<Object>, String, String) |
Asserts that the delegate |
| Throws<TException>(Func<Object>, String, Object[]) |
Asserts that the delegate |
| Throws<TException>(Func<Object>, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>, String) |
Asserts that the delegate |
| Throws<TException>(Action, String, Object[]) |
Asserts that the delegate |
| Throws<TException>(Action, String, String) |
Asserts that the delegate |
| Throws<TException>(Action, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>, String) |
Asserts that the delegate |
| Throws<TException>(Func<Object>, Func<Exception,String>) |
Asserts that the delegate |
| Throws<TException>(Func<Object>, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>) |
Asserts that the delegate |
| Throws<TException>(Action, Func<Exception,String>) |
Asserts that the delegate |
| Throws<TException>(Action, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>) |
Asserts that the delegate |
| Throws<TException>(Action, Func<Exception,String>, String) |
Asserts that the delegate |
Throws<TException>(Func<Object>, Func<Exception,String>, String)
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Func<object?> action, Func<Exception?,string> messageBuilder, string actionExpression = "") where TException : Exception;
static member Throws : Func<obj> * Func<Exception, string> * string -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Func(Of Object), messageBuilder As Func(Of Exception, String), Optional actionExpression As String = "") As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
A func that takes the thrown Exception (or null if the action didn't throw any exception) to construct the message to include in the exception when action does not throws exception of type TException.
- actionExpression
- String
The syntactic expression of action as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Func<Object>, String, String)
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Func<object?> action, string? message = "", string actionExpression = "") where TException : Exception;
static member Throws : Func<obj> * string * string -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Func(Of Object), Optional message As String = "", Optional actionExpression As String = "") As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- message
- String
The message to include in the exception when action does not throws exception of type TException.
- actionExpression
- String
The syntactic expression of action as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Func<Object>, String, Object[])
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Func<object?> action, string message = "", params object[] messageArgs) where TException : Exception;
static member Throws : Func<obj> * string * obj[] -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Func(Of Object), Optional message As String = "", ParamArray messageArgs As Object()) As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- message
- String
The message to include in the exception when action does not throws exception of type TException.
- messageArgs
- Object[]
An array of parameters to use when formatting message.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Func<Object>, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>, String)
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Func<object?> action, ref Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException> message, string actionExpression = "") where TException : Exception;
static member Throws : Func<obj> * AssertNonStrictThrowsInterpolatedStringHandler * string -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Func(Of Object), ByRef message As Assert.AssertNonStrictThrowsInterpolatedStringHandler(Of TException), Optional actionExpression As String = "") As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
The message to include in the exception when action does not throws exception of type TException.
- actionExpression
- String
The syntactic expression of action as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Action, String, Object[])
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Action action, string message = "", params object[] messageArgs) where TException : Exception;
static member Throws : Action * string * obj[] -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Action, Optional message As String = "", ParamArray messageArgs As Object()) As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- action
- Action
Delegate to code to be tested and which is expected to throw exception.
- message
- String
The message to include in the exception when action does not throws exception of type TException.
- messageArgs
- Object[]
An array of parameters to use when formatting message.
Returns
The exception that was thrown.
Exceptions
Thrown if action does not throws exception of type TException.
Applies to
Throws<TException>(Action, String, String)
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Action action, string? message = "", string actionExpression = "") where TException : Exception;
static member Throws : Action * string * string -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Action, Optional message As String = "", Optional actionExpression As String = "") As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- action
- Action
Delegate to code to be tested and which is expected to throw exception.
- message
- String
The message to include in the exception when action does not throws exception of type TException.
- actionExpression
- String
The syntactic expression of action as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Returns
The exception that was thrown.
Exceptions
Thrown if action does not throws exception of type TException.
Applies to
Throws<TException>(Action, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>, String)
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Action action, ref Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException> message, string actionExpression = "") where TException : Exception;
static member Throws : Action * AssertNonStrictThrowsInterpolatedStringHandler * string -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Action, ByRef message As Assert.AssertNonStrictThrowsInterpolatedStringHandler(Of TException), Optional actionExpression As String = "") As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- action
- Action
Delegate to code to be tested and which is expected to throw exception.
The message to include in the exception when action does not throws exception of type TException.
- actionExpression
- String
The syntactic expression of action as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Func<Object>, Func<Exception,String>)
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Func<object?> action, Func<Exception?,string> messageBuilder) where TException : Exception;
static member Throws : Func<obj> * Func<Exception, string> -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Func(Of Object), messageBuilder As Func(Of Exception, String)) As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
A func that takes the thrown Exception (or null if the action didn't throw any exception) to construct the message to include in the exception when action does not throws exception of type TException.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Func<Object>, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>)
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Func<object?> action, ref Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException> message) where TException : Exception;
static member Throws : Func<obj> * AssertNonStrictThrowsInterpolatedStringHandler -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Func(Of Object), ByRef message As Assert.AssertNonStrictThrowsInterpolatedStringHandler(Of TException)) As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
The message to include in the exception when action does not throws exception of type TException.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Action, Func<Exception,String>)
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Action action, Func<Exception?,string> messageBuilder) where TException : Exception;
static member Throws : Action * Func<Exception, string> -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Action, messageBuilder As Func(Of Exception, String)) As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- action
- Action
Delegate to code to be tested and which is expected to throw exception.
A func that takes the thrown Exception (or null if the action didn't throw any exception) to construct the message to include in the exception when action does not throws exception of type TException.
Returns
The exception that was thrown.
Exceptions
Thrown if action does not throws exception of type TException.
Applies to
Throws<TException>(Action, Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException>)
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Action action, ref Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AssertNonStrictThrowsInterpolatedStringHandler<TException> message) where TException : Exception;
static member Throws : Action * AssertNonStrictThrowsInterpolatedStringHandler -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Action, ByRef message As Assert.AssertNonStrictThrowsInterpolatedStringHandler(Of TException)) As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- action
- Action
Delegate to code to be tested and which is expected to throw exception.
The message to include in the exception when action does not throws exception of type TException.
Returns
The exception that was thrown.
Applies to
Throws<TException>(Action, Func<Exception,String>, String)
- Source:
- Assert.ThrowsException.cs
Asserts that the delegate action throws an exception of type TException
(or derived type) and throws AssertFailedException if code does not throws exception or throws
exception of type other than TException.
public static TException Throws<TException>(Action action, Func<Exception?,string> messageBuilder, string actionExpression = "") where TException : Exception;
static member Throws : Action * Func<Exception, string> * string -> 'Exception (requires 'Exception :> Exception)
Public Shared Function Throws(Of TException As Exception) (action As Action, messageBuilder As Func(Of Exception, String), Optional actionExpression As String = "") As TException
Type Parameters
- TException
The type of exception expected to be thrown.
Parameters
- action
- Action
Delegate to code to be tested and which is expected to throw exception.
A func that takes the thrown Exception (or null if the action didn't throw any exception) to construct the message to include in the exception when action does not throws exception of type TException.
- actionExpression
- String
The syntactic expression of action as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Returns
The exception that was thrown.
Exceptions
Thrown if action does not throws exception of type TException.