Eric J.
Eric J.

Reputation: 150108

VS2010 Debugger Breaking on Handled Exception

I have MS Test unit tests that ensure that an Exception is thrown when the method under test is given bad arguments. I'm using the pattern:

My actual;
bool threw = false;
try
{
    actual = target.DoSomething(aBadParameter);
}
catch
{
    threw = true;
}

Assert.IsTrue(threw);

I have CLR Exceptions set to break only when user-unhandled (not when thrown). When DoSomething() throws a new Exception(), however, the debugger breaks. If I resume, the unit test completes successfully.

If I cut-and-paste the unit test code into the main program and run it in the context of the main program (instead of under MS Test), the debugger does not break at the user-handled Exception.

How can I prevent the debugger from breaking on my user-handled Exceptions?

This does not appear on the surface related to

Getting an Unhandled Exception in VS2010 debugger even though the exception IS handled

because in that case the Exception was being thrown on a different thread and was being rethrown by the CLR inside a callback.

Upvotes: 1

Views: 155

Answers (1)

BrokenGlass
BrokenGlass

Reputation: 160852

The idiomatic way to test for thrown exceptions in MSTest is using the ExpectedException attribute:

[TestMethod]
[ExpectedException(typeof(FooException))]
public void ThrowsFooExceptionWithBadInput()
{
     var actual = target.DoSomething(aBadParameter);
}

Upvotes: 2

Related Questions