none
Breaking on exceptions in VS2010

    Question

  • I have run into an interesting issue using the debugger in Visual Studio 2010 and the .NET Framework 4.  I use NUnit for my unit testing.  I was just testing to ensure everything still works after upgrading to VS 2010.  I usually run NUnit by specifying it as the Start External Program option on the Debug setup on my unit test assembly.  This causes the debugger to auto attach to NUnit.

    Anyhow I have a number of unit tests which test that exceptions are properly thrown.  NUnit catches these exceptions to ensure they are expected.  In previous versions of Visual Studio (2008, 2005, 2003, 2002, I've used them all this way) and even in Visual Studio 2010 if I target the 3.5 framework or earlier when my tests are run everything is fine and the debugger doesn't break on the exceptions I'm testing are thrown because NUnit catches them.

    In Visual Studio 2010 with .NET 4 the debugger breaks on the exceptions being thrown.  In doing some googling I saw reference to a new debugger feature which auto breaks on exceptions thrown when the method was invoked via reflection.  That is obviously what NUnit is doing to run my tests, invoking them via reflection.  The problem is I don't want the debugger to break on these exceptions even though they occur on a method called via reflection.

    At the url: http://msdn.microsoft.com/en-us/library/85yzd16c.aspx.  It mentions an option that appears to control this

    "Break when exceptions cross AppDomain or managed/native boundaries"

    3) When code calls a function by using reflection, and the function throws an exception. For more information about reflection, see Reflection.

    I have disabled this option but yet the debugger is still breaking and I really don't want it to.  Is this a bug that the option isn't being honored or am I missing something else?  As I mentioned this only occurs if the assembly I am debugging targets the .NET Framework 4.  If I target and earlier framework I don't have this problem.

     

    Any help would be greatly appreciated.

     

    Bryan

    Saturday, April 17, 2010 3:48 AM

Answers

  • This is not a bug in the debugger, it was a intentional change.  While the behavior is different and can be confusing, it had to be changed in Visual Studio 2010 to support several other scenarios including Silverlight debugging.  There are three workarounds (one of which Hongye already shared).

    1.  Disable "break on user unhandled exceptions" for the exception types you you are encountering here (via Debug -> Exceptions)
    2.  Disable "break on user unhandled exceptions" for all exceptions (via Debug -> Exceptions)
    3.  Disable "Just My Code"


    Best Regards, Andrew Hall. Visual Studio Debugger.
    Friday, April 23, 2010 1:27 AM
    Moderator
  • One of the key driving factors for changing behavior is customer feedback, please file a suggestion to change this behavior at https://connect.microsoft.com/VisualStudio/feedback/CreateFeedbackForm.aspx


    Best Regards, Andrew Hall. Visual Studio Debugger.
    Friday, April 23, 2010 7:47 PM
    Moderator

All replies

  • Hi Bryan,

    Thanks for reporting this issue.

    I have tested the feature in MSTest. However, the behavior is as expected:

    When "Break when exceptions cross AppDomain or managed/native boundaries" is checked, Visual Studio will break when exception is thrown. But if it is cleared, VS won't break.

    Can you check if you have any checkbox checked in the Debug menu / Exception dialog?

    In order to narrow down this issue, can you write a simple application which uses reflection to invoke another method and throw exception in the invoked method?

    You'd better reset your Visual Studio settings before testing. Tools / Import and Export Settings / Reset all settings.

    Thanks


    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    Monday, April 19, 2010 8:10 AM
    Moderator
  • I have produced a simple application which shows this problem.  I have also double checked my debugging settings and have screenshots which I could show of my settings.  I don't see a way to attach the sample file or screenshots via the forum.  One thing I did observe is if I uncheck under exceptions the break on User-unhandled exceptions then I don't observe this problem.  I confirmed though under Visual Studio 2008 I have that setting checked and I don't observe this behavior.

     

    I do want to break on User-unhandled exceptions under most circumstances but if there is a try block around a reflection invoked method I would consider that to not be a user-unhandled exception.  It is under all previous versions of Visual Studio. 

    Since I can't see a way to attach a file I'm copying the code from my sample project below:

     

    class Program
      {
        static void Main(string[] args)
        {
          TestClass aClass = new TestClass();
    
          MethodInfo method = aClass.GetType().GetMethod("TestAMethod");
    
          try
          {
            method.Invoke(aClass, null);
          }
          catch (TargetInvocationException ex)
          {
            Console.Out.Write("Exception Handled");
          }
    
          Console.In.ReadLine();
    
          
        }
      }
    
     public class TestClass
      {
        public void TestAMethod()
        {
          throw new ApplicationException("An Exception");
        }
      }

    Monday, April 19, 2010 4:46 PM
  • Hello Bryan,

    Thanks for the repro code. I was able to reproduce this issue after resetting the VS settings to default settings.

    An interesting finding is that the title of the exception dialog is "XXXException was unhandled by user code". However an actual unhandled exception will have a title of "XXXException was unhandled".

    When I clear the setting of "Enable Just My Code", the debugger won't break.

    I suspect that it is a bug in VS 2010 debugger. It tries to figure out user handled and non-user handled exception, but it fails to figure out in reflection case or other indirectly call cases.

    I suggest you to report a bug at http://connect.microsoft.com to confirm with product group. Currently you can clear "Enable Just My Code" to workaround the issue. Thanks.


    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    • Proposed as answer by MotJuste Thursday, March 10, 2011 8:29 PM
    Wednesday, April 21, 2010 2:31 AM
    Moderator
  • This is not a bug in the debugger, it was a intentional change.  While the behavior is different and can be confusing, it had to be changed in Visual Studio 2010 to support several other scenarios including Silverlight debugging.  There are three workarounds (one of which Hongye already shared).

    1.  Disable "break on user unhandled exceptions" for the exception types you you are encountering here (via Debug -> Exceptions)
    2.  Disable "break on user unhandled exceptions" for all exceptions (via Debug -> Exceptions)
    3.  Disable "Just My Code"


    Best Regards, Andrew Hall. Visual Studio Debugger.
    Friday, April 23, 2010 1:27 AM
    Moderator
  • I don't understand why this change would have been made.  It makes Visual Studio's support for unit testing much worse.  My understanding was that Microsoft was trying to embrace Test Driven Design and make unit testing more feasible with Visual Studio 2010.  This does not do that.

    While the workarounds may work they have unacceptable side effects.  I don't want to disable the "break on user unhandled exceptions" or "Just My Code" for all my projects yet it seems those are my only two options.  I want those enabled when I'm debugging my GUI though it appears I have to turn them off to make unit testing feasible.

    From what I can see there isn't a way to apply these settings on a per project basis.  I have to adjust them at a Visual Studio level and therefore have to change them before each time I start debugging.  That is not an acceptable or reasonable solution.

    This looks very much like a bug to me, maybe an intentional one but like much of Vista it makes things unusable.

    Any chance this behavior will be changed back to the way it was which worked very well?

    If not is there a chance we can get the debugging settings applied on a per project basis so we don't have to adjust them every time we start a project?

    Or are there other options which would solve the problem that haven't been described yet?

    I like a lot of the things about Visual Studio 2010 but I've got to come up with a solution that works for this problem or it is going to kill productivity to move to Visual Studio 2010 for me and others who do unit testing.

     

    Friday, April 23, 2010 2:48 PM
  • Yes there is a chance that this confusing behavior will be fixed, we are currently investigating the options for doing so
    Best Regards, Andrew Hall. Visual Studio Debugger.
    Friday, April 23, 2010 7:45 PM
    Moderator
  • One of the key driving factors for changing behavior is customer feedback, please file a suggestion to change this behavior at https://connect.microsoft.com/VisualStudio/feedback/CreateFeedbackForm.aspx


    Best Regards, Andrew Hall. Visual Studio Debugger.
    Friday, April 23, 2010 7:47 PM
    Moderator
  • Hi Bryan,

    Did you file a bug on Microsoft Connect for this one? I find this behaviour extremely annoying. All the unit tests that tests exceptional behaviour (test having the ExpectedException attribute) now makes the debugger break, even if the test is successful.  

    Nils 

     

     

    Thursday, June 10, 2010 8:58 AM
  • I haven't had a chance to yet.  I've been using the workaround and dealing with it.  If you create one I think there is a place I can vote for it to give it better visibility and I will do so.
    Thursday, June 10, 2010 2:23 PM
  • I don't use NUnit, I use the testing stuff (not sure what it is called) that comes with VS. In 2008 tests that had 'Assert.inconclusive' statement, VS just marked the tests status so. Now I get the AssertInconclusivException unhandled dialog. That feels like a bug to me.
    Tuesday, March 08, 2011 10:23 AM
  • Looks like a bug to me.
    Wednesday, August 22, 2012 12:44 AM