Prevent Visual Studio from breaking when throwing exceptions

11,048

Solution 1

I had same problem when I started using VS2010. I have unit tests, which expect exceptions, and I throw exceptions from my functions. These exceptions are supposed to be handled by the user of my library. In Debug->Exceptions dialog, I unchecked check box under User-Unhandled column for Common Language Runtime Exceptions, and VS stopped breaking on these exceptions. By the way, I don't see second column in the dialog you attached here.

Solution 2

If you throw an exception that is not handled anywhere in your code, Visual Studio is going to break. It doesn't have any other choice: there was an unhandled exception. Outside of Visual Studio, the application would show an error message and inform the user that an unhandled exception occurred.

The options you see in the Debug -> Exceptions dialog only allow you to configure whether Visual Studio breaks on all exceptions, including those that are later handled in your code. These are often referred to as "first-chance" exceptions.

Beyond that, you should never throw a NullReferenceException yourself; this is a runtime exception that is reserved for the runtime framework. Instead, you should throw an ArgumentNullException.

Solution 3

The below method works for me in Visual Studio 2015 (a similar process may work for VS2010).

Taken from the Visual Studio documentation on managing exceptions with the debugger:

  1. In the Exception Settings window, open the context menu by right-clicking in window and then selecting Show Columns. (If you have turned off Just My Code, you will not see this command.)
  2. You should see a second column named Additional Actions. This column displays Continue when unhandled by user code on specific exceptions, meaning that the debugger does not break if that exception is not handled in user code but is handled in external code.
  3. You can change this setting either for a particular exception (select the exception, right-click, and select/deselect Continue when Unhandled in User Code) or for an entire category of exceptions (for example, all the Common Language Runtime exceptions).
Share:
11,048

Related videos on Youtube

serhio
Author by

serhio

I like .NET and started learning PHP. email: gserhio[at]gmail[.]com

Updated on June 04, 2022

Comments

  • serhio
    serhio almost 2 years

    I test the exceptions interception, so, I don't need that Visual Studio breaks on thinkgs like thrown new NullReferenceException("myVar").

    I have the following under Debug=>Exceptions

    enter image description here

    however, VS breaks on the exceptions. What should I do?

    PS.

    for the application unhandled exception, I "catch" them using the Application.UnhandledException as in the the following:

    ''' <summary>Occurs when the application encounters an unhandled exception.</summary> '
    Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
      Dim message As String = String.Format("An application UnhandledException were thrown.{1}The application will now terminate.{1}'{0}'{1}{1}StackTrace:{1}{2}", e.Exception.Message, Environment.NewLine, e.Exception.StackTrace)
      MessageBox.Show(message)
    End Sub
    
  • serhio
    serhio about 13 years
    See my PS. As for your remark, maybe you have reason, anyway, MSDN just informs Note that applications throw the ArgumentNullException exception rather than the NullReferenceException exception...
  • Summer Sun
    Summer Sun about 11 years
    FYI, the debugging option "Enable Just My Code (Managed only)" must be checked in order to see the "User-unhandled" column in the Exceptions window.

Related