New to Telerik JustMockStart a free 30-day trial

Throws

Updated on Jan 27, 2026

The Throws method is used to throw an exception when a given call is made. This topic goes through a number of scenarios where the Throws method is useful.

Here is the system under test for these examples:

C#
public interface IFoo
{
    string Execute(string myStr);
}

Throw Exception on Method Call

Change a method behavior to throw an exception once it is called.

C#
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowExceptionOnMethodCall()
{
    // Arrange
    var foo = Mock.Create<IFoo>();

    Mock.Arrange(() => foo.Execute(string.Empty)).Throws<ArgumentException>();

    // Act
    foo.Execute(string.Empty);
}

The assert step is done via the ExpectedException attribute, where we explicitly specify that an exception of type ArgumentException must be thrown during the execution of the test.

Throw Exception with Arguments on Method Call

Change a method behavior to throw an exception once it is called and pass arguments to the exception.

C#
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldThrowExceptionWithArgumentsOnMethodCall()
{
  // Arrange
  var foo = Mock.Create<IFoo>();

  Mock.Arrange(() => foo.Execute(string.Empty)).Throws<ArgumentException>("Argument shouldn't be empty.");

  // Act
  foo.Execute(string.Empty);
}

The assert step is done via the ExpectedException attribute, where we explicitly specify that an exception of type ArgumentException must be thrown during the execution of the test. Calling foo.Execute with empty string will result in throwing an exception and passing "Argument shouldn't be empty." to it.

See Also