Breakpoint on an exception in visual studio - visual-studio

How do I set a breakpoint on an exception?
Ideally I want to be able to see the call stack and local variables from the code that threw the exception.

I haven't used Visual Studio for 2 years, but from memory:
Ctrl + Alt + E
Will bring exception management screen, make sure to check break on all exceptions, a must have imho :)
EDIT : My memory not that bad :) Just check Thrown on Common Language Runtime Exceptions if your are using .NET (CLR = .NET)
EDIT 2 : By the way, since exceptions are generally a bad coding practice and should be avoided as possible, i suggest to let that option enabled all the time. If some exceptions just can't be avoided (because of someone else, of course :D) just uncheck its type or ancestor in the exception type tree.

The exact visual studio command name is Debug.Exceptions
Main Menu > Debug > Exceptions (when solution is open)

Also, make sure you're running in debug mode and that debugging is enabled in the project properties or else all the breakpoints in the world will count for nothing.
(Depending on your environment you can also click on the margin outside the code source to insert a breakpoint via mouse action)

Related

How to not break when user handled exception is thrown in visual studio 2019

Microsoft Visual Studio Enterprise 2019
Version 16.5.5
VisualStudio.16.Release/16.5.5+30104.148
Microsoft .NET Framework
Version 4.8.03752
I don't want visual studio to break in debug mode when such as below exceptions happened. The ones that i have handled via try catch. But i could not find a way even though I did extensive internet search
project details : asp.net web forms, .net 4.8 framework
How to not break when user handled exception is thrown in visual
studio 2019
First, thanks to Jazimov for sharing the wonderful suggestion.
Actually, to stop breaking the specific exception(System.NullReferenceException) during Debug mode, you should try Jazimpv's suggestion.
Debug-->Windows-->Exception Settings-->Common Language Runtime Exceptions
uncheck System.Null.ReferenceException
This feature simply prevents exceptions from interrupting debugging, but does not block the occurrence of exceptions. Although it does not appear in the Code Editor, it will also be caught by the output window.
However, you can't get the most straightforward exception directly in the code editor without interrupting the debugging mode.
In order to get more detailed exception information, you can write this to show on Output Window:
try
{
.........
}
catch(Exception ex)
{
Debug.WriteLine("=============================");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.Source);
Debug.WriteLine(ex.StackTrace);
Debug.WriteLine("=============================");
}
I had a similar issue and found the fix here: Tell the debugger to continue on user-unhandled exceptions
Basically, in Exception Settings > Addition Actions column on Common Language Runtime Exceptions row, I right clicked and selected "Continue when unhandled in user code". i didn't see it break since.
You should be able to uncheck the checkbox next to "Break when this exception type is thrown" and that particular exception--when thrown--no longer should cause Visual Studio to break. Checking this checkbox does not generically prevent exceptions: You are preventing the debugger from breaking when this specific kind of exception is thrown. Notice that you also can restrict whether the debugger breaks for the particular exception to particular assemblies by checking boxes under the "Except when thrown" heading in the dialog you cited.
To view which exceptions are thrown by the debugger, go to Visual Studio's Debug | Windows | Exception Settings menu and expand the Common Language Runtime Exceptions branch:

Why The application is in break mode?

I swear I used to remember Visual Studio just breaking on an exception. It would take you straight to the line where the exception was thrown and allowed you to inspect variables just by hovering over them.
Or maybe that was a dream. Or maybe this is a nightmare.
But it's actually not a problem, right, because now instead of it just working all you need to do is go to Debug > Windows > Exception Settings where you get this mess.
Which has many glaring problems. For example:
Remember when I said it used to just work?
I don't have all these Exception types memorized.
Clicking toggle all on/off removes your previous selections.
Oh and also, clicking this checkbox does absolutely nothing
It used to just work. Is it possible to get it back to just doing that?
Under exception settings, Go to Common Language Runtime Exceptions->check all. This will not break your application, but show you the exact line of occurrence of the exception.
Per that first image you posted, it appears the application is throwing an exception in "external" code, so you likely have the "Just My Code" enabled in the debugging options. Try unchecking that "Just My Code" option via your Tools|Options dialog (Debugging\General category), and that'll likely fix you up.

How to make visual studio break only on unhandled exceptions?

On my other machines, Visual Studio always broke on errors when there was not a try/catch to handle them, but if there was a try/catch then it didn't break.
For some reason, on this laptop, it doesn't work that way. It didn't break at all at first, but then I found out how to set it to break by going to debug/exceptions. However, configuring it to break there causes it to always break on exceptions even if there is a try/catch block.
How do I make it work like I'm used to?
Make sure you have Just My Code Enabled by going into Tools-->Options-->Debugger-->General--> Enable Just My Code. This will change your Debug--> Exceptions Dialog Box to show a CheckBox for User-unhandled Errors.
I cannot find the dialogue in the accepted answer.
In my experience, in Exception Settings, if you hit "Restore the list to default settings", it will not break on exceptions you handled. If you checked a particular exception in Exception Settings, then it will break regardless of whether you handles this exception in your code or not.
For a more updated answer:
When you go to the exception settings right click on the exception type you want, which for C# would probably be Common Language Runtime Exceptions, and enable the "Continue When Unhandled in User Code" setting.
For me this seemed to not break on exceptions that were handled in a try catch or otherwise, but it did break when an unhandled exception occurred. The naming of the option makes it feel a bit iffy but it seems to work exactly as you and I hoped now.
Exception settings
Enable the required setting
In Visual Studio 2022 in the Exception Settings (Debug > Windows > Exception Settings), there is another column (Additional Actions) that can be viewed. It is written about here in the Microsoft Docs.
Make sure to remove the "Continue when unhandled in user code" setting in order break on any given exception.

Configure the visual studio debugger for try-catch statements

VS 2005
For example,
My employees gave me a project with about X try-catch statements.
X > 100 .. 300
I need to test a project. Is there a way to mark each (every) beginning of catch as a breakpoint ? I don't want to do it manually. Maybe there is some settings that fit to me ?
Go to Debug > Exceptions (Visual Studio 2013 and earlier) or Debug > Windows > Exception Settings (Visual Studio 2015 and later).
In this dialog you can enable first chance debugging of exceptions - when an exception is thrown, the debugger will automatically break at the throwing code before the "catch" code is executed, allowing you to debug it.
What you want to do is ask it to break when CLR exceptions are thrown, not only when they're unhandled (image from Visual Studio 2013 - 2015 is similar but now is in a view rather than a dialog):
(Note: This won't get the debugger to break whenever you execute a try block, only if the exception is actually thrown)
Short answer is no. But you might be able to make some an aspect-oriented plugin to your project that captures the catch crosspoint, then you just have to put one breakpoint at in your aspect
I am not aware of a possibility that allows setting breakpoints in code by some pattern. The closest you can come to is Debug/New breakpoint/Break at Function where you can specify the file and line number. If you can get this automated and working down a list generated by a grep search, you might find a way. Here is something from the IDE samples that might get you started:
' Sets a pending breakpoint at the function named "main". It marks the
' breakpoint as one set by automation.
Sub AddBreakpointToMain()
Dim bp As EnvDTE.Breakpoint
Dim bps As EnvDTE.Breakpoints
bps = DTE.Debugger.Breakpoints.Add("main")
For Each bp In bps
bp.Tag = "SetByMacro"
Next
End Sub
However, why do you want to set those breakpoints anyway? If it's in order to catch exceptions as they are thrown, you can make the debugger break automatically whenever the happens under Tools/Exceptions.
You can use this if you are using older IDE's (anything pre-2012)
Programmatically apply / deactivate breakpoints in Visual Studio
Unfortunately they removed the macros from the newer IDE's.
There are extensions you can download and one of them allows you to modify a *.js file. Issue is going to be converting what the gentleman wrote in the other post to have it read properly.
For now I'm just using System.Diagnostics.Debugger.Launch(); it's just a pain and it would be awesome if someone could translate that file over.

Any way in Visual Studio to not break on throwing of a specific exception?

Is there a pragma or debugger attribute which will allow the debugger to not break on the throwing of a specific exception even though under the Debug >> Exceptions menu I've told it to break when any CLR Exceptions are throw?
In general while developing I like to have it break on exceptions while debugging so that I can immediately inspect them. Sometimes there are some isolated cases where it is known that this block of code occasionally throws exceptions and I've handled it in with a try-catch. See the answer to this question where the consensus was that try-catch is the most correct situation.
I'd like to be able to set an attribute on the method (something analogous to System.Diagnostics.DebuggerHiddenAttribute) which just ignores any exceptions thrown in the method.
BTW, I'm currently experiencing this in Visual Studio 2008, but I'm guessing there is either an answer for all versions or none.
The direct answer can be found under Exceptions menu item of the Debug menu. This is a per solution/project setting. (Tools > Option > Debugging is a system-wide setting.) See the help topic Visual Studio Debugger, How to: Break When an Exception is Thrown at http://msdn.microsoft.com/en-us/library/d14azbfh.aspx for details. The Exceptions dialog allows you to set which exceptions are thrown or which exceptions break into the debugger.
I find I get more use out of the DebuggerStepThrough attribute.
In general, I leave throwing exceptions to the default (Debug > Exceptions user-unhandled checked and Thrown unchecked) and add the DebuggerStepThrough attribute for methods where I am not interested in stepping through nor am I interested in any exceptions being thrown within that method. I rarely use DebuggerHidden, and get more use with DebuggerNonUserCode in library code.

Resources