title: NUnit: Why Doesn't Assert.Throws<T> Catch My ArgumentNullException?
body: <p>I am posting this question anew at the behest of the distinguished Mr. John Skeet, who suggested I devise a simple test program that isolates and demonstrates the issue I am encountering and repost the question. This question grew out of <a href="https://stackoverflow.com/questions/4761738/nunit-neither-assert-throws-nor-expectedexception-catch-thrown-exception">this one</a>, so please forgive me if it all sounds very familiar. You can potentially glean extra details about this question from that one.</p>
  
  <p>The issue I am encountering regards <code>Assert.Throws&lt;T&gt;</code> from NUnit 2.5.9. It will, on occasion, fail to catch <em>any</em> exceptions thrown in the method invoked by the TestDelegate. I have pinned down this behavior in a reproducible manner in the code below. (Though this may, admittedly, be a case of Fails On My Machine™.</p>
  
  <p>To reproduce the error, I've created a solution with two C# DLL projects:</p>
  
  <ul>
  <li>The first contains one class, with a single public method. That method is an extension method that encapsulates the logic required to create a <code>SqlCommand</code>, populate its parameters and invoke <code>ExecuteScalar</code> on it. This project includes no other references.</li>
  <li>The second contains a single class with two methods that test whether or not the method in the first DLL is working as expected. This project references the first, and includes a reference to the NUnit Framework. No other assemblies are referenced.</li>
  </ul>
  
  <p>When I step through the tests in the debugger, I observe the following:</p>
  
  <ol>
  <li><code>Assert.Throws</code> correctly invokes the <code>ExecuteScalar&lt;T&gt;</code> extension method. </li>
  <li>The parameter values are null, as expected.</li>
  <li><code>ExecuteScalar&lt;T&gt;</code> tests its parameters for null values.</li>
  <li>The debugger does hit and execute the line containing <code>throw new ArgumentNullException(...)</code>.</li>
  <li>After executing the <code>throw</code>, control of the application is not immediately transferred to <code>Assert.Throws</code>. Instead, it continues on the next line in <code>ExecuteScalar&lt;T&gt;</code>.</li>
  <li>As soon as the next line of code executes, the debugger breaks, and displays the error "Argument null exception was unhandled by user code."</li>
  </ol>
  
  <p>The source code that isolates this behavior is given below.</p>
  
  <p><strong>THE EXTENSION METHOD</strong></p>
  
  <pre><code>namespace NUnit_Anomaly
  {
      using System;
      using System.Data;
      using System.Data.SqlClient;
  
      public static class Class1
      {
          public static T ExecuteScalar&lt;T&gt;(this SqlConnection connection, string sql)
          {
              if (connection == null)
              {
                  throw new ArgumentNullException("connection");
              }
  
              if (sql == null)
              {
                  throw new ArgumentNullException("sql");
              }
  
              using (var command = connection.CreateCommand())
              {
                  command.CommandType = CommandType.Text;
                  command.CommandText = sql;
                  return (T)command.ExecuteScalar();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>THE TEST CASES</strong></p>
  
  <pre><code>namespace NUnit_Tests
  {
      using System;
      using System.Data.SqlClient;
      using System.Diagnostics;
  
      using NUnit.Framework;
  
      using NUnit_Anomaly;
  
      [TestFixture]
      public class NUnitAnomalyTest
      {
  
          [Test]
          public void ExecuteDataSetThrowsForNullConnection()
          {
              Assert.Throws&lt;ArgumentNullException&gt;(() =&gt; ((SqlConnection)null).ExecuteScalar&lt;int&gt;(null));
          }
  
          [Test]
          public void ExecuteDataSetThrowsForNullSql()
          {
  
              const string server = "MY-LOCAL-SQL-SERVER";
              const string instance = "staging";
              string connectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;",
                                                      server,
                                                      instance);
  
              using (var connection = new SqlConnection(connectionString))
              {
                  Assert.Throws&lt;ArgumentNullException&gt;(() =&gt; connection.ExecuteScalar&lt;int&gt;(null));
              }
          }
      }
  }
  </code></pre>
  
  <p>The net effect is that the tests fail when they shouldn't. To the best of my understanding, <code>Assert.Throws&lt;T&gt;</code> should catch my exception and the test should pass.</p>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>I took Hans' advice and checked the Exceptions dialog. I wasn't breaking on <em>thrown</em> exceptions, but I was breaking on <em>unhandled user</em> exceptions. Apparently, that's why the debugger breaks into the IDE when the exception is thrown. Clearing the checkbox fixed the problem, and <code>Assert.Throws&lt;T&gt;</code> picked it up. However, if I haven't done this, I can't just press F5 to continue execution, or the exception will become a <code>NullReferenceException</code>. </p>
  
  <p>So now the question is: Can I configure exception breaks on a per-project basis? I only want to do this when I'm testing, but not in general.</p>