A Better Way to Test for Exceptions in Visual Studio

An exception is an indication of an error state that happens whereas a program is being executed. You’ll be able to tell the debugger which exceptions or sets of exceptions to break on, and at which point you need the debugger to break. When the debugger breaks, it appears you where the Exception was tossed. You’ll be able to include or erase exceptions.With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window.< Provide handlers that respond to the foremost vital Exceptions. Moreover, learn how to design the debugger to continuously break execution for a few Exceptions. When an exception happens, the debugger composes an exception message to the Output window. It may break execution within the following cases when:

  • An exception is thrown that isn’t handled
  • The debugger is configured to break execution before any handler is invoked
  • The debugger is configured to break execution before any handler is invoked

An exception is an indication of an error state that occurs while a program is being executed. You can tell the debugger which exceptions or sets of exceptions to break on, and at which point you want the debugger to break. When the debugger breaks, it shows you where the exception was thrown. You can also add or delete exceptions.

The debugger can break execution at the point where an exception is thrown, so you may examine the exception before a handler is invoked.

In the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.AccessViolationException. You can also select an entire category of exceptions.
In case you select an exception within the Exception Settings window, debugger execution will break wherever the Exception is tossed, no matter whether it’s taken care of. Presently the exception is called a, to begin with, the chance Exception. For example, here are some of the scenarios:

In the following C# console application, the Main method throws an AccessViolationExceptioninside a try/catch block.

static void Main(string[] args)
{
try
{
throw new AccessViolationException();
Console.WriteLine(“here”);
}
catch (Exception e)
{
Console.WriteLine(“caught exception”);
}
Console.WriteLine(“goodbye”);
}

If you have AccessViolationException checked in Exception Settings, execution will break on the throw line when you run this code in the debugger. You can then continue execution. The console should display both lines:
public class Class1
{
public void ThrowHandledException()
{
try
{
throw new AccessViolationException();
}
catch (AccessViolationException ave)
{
Console.WriteLine(“caught exception” + ave.Message);
}
}

public void ThrowUnhandledException()
{
throw new AccessViolationException();
}
}

If you have AccessViolationException checked in Exception Settings, execution will break on the throw line in both ThrowHandledException() and ThrowUnhandledException() when you run this code in the debugger.

To restore the exception settings to the defaults, choose the Restore the list to the default settingsbutton:

Tell the debugger to continue on user-unhandled exceptions

If you are debugging .NET or JavaScript code you can tell the debugger to prevent breaking on exceptions that aren’t handled in user code but are handled elsewhere.

  • In the Exception Settings window, open the shortcut menu by right-clicking a column label, and then select Show Columns > Additional Actions. (If you’ve turned off Just My Code, you won’t see this command.) A third column named Additional Actions appears.
  • To change this setting for a specific exception, select the exception, right-click to appear the easy route menu, and select Continue When Unhandled in User Code. You will also change the setting for a whole category of exceptions, such as the complete Common Language Runtime Exception.

Add and delete exceptions

You’ll include and delete exceptions. To erase an exception sort from a category, select the exception, and select the Delete the selected exception from the list button (the minus sign) on the Exception Settings toolbar. Or you may right-click the exception and select Delete from the shortcut menu. Deleting an exception has the same effect as having the exception unchecked, which is that the debugger won’t break when it’s thrown.

To add an exception:

  • In the Exception Settings window, select one of the exception categories (for example, Common Language Runtime).
  • 2. Choose the Add an exception to the selected category button (the plus sign).The exception is added to the list (in alphabetical order) and automatically checked. To add an exception to the GPU Memory Access Exceptions, JavaScript Runtime Exceptions, or Win32 Exceptions categories, include the error code and the description.

Add conditions to an exception

Use the Exception Settings window to set conditions on exceptions. Currently supported conditions incorporate the module name(s) to incorporate or avoid for the special case. By setting module names as conditions, you’ll select to break for the exception only on certain code modules. You’ll moreover select to maintain a strategic distance from breaking on specific modules.

To add conditional exceptions:

  • Choose the Edit conditions button in the Exception Settings window, or right-click the exception and choose Edit Conditions.
  • To add extra required conditions to the exception, select Add Condition for each new condition. Additional condition lines appear.
  • For each condition line, type the name of the module, and change the comparison operator list to Equals or Not Equals. You may specify wildcards (\*) in the name to specify more than one module.
  • If you need to delete a condition, choose the X at the end of the condition line.

Leave a Comment