I frequently use the $exception pseudovariable to gain access to the items in theUpdateException.StateEntries collection as they are not accessible via the Exception Assistant's 'View Details' dialog.
Adding a watch with the name '$exception' will return the current exception. This is also available automatically in the Locals window for C# if the exception assistant is disabled.
I have seen cases where this variable is not available. Why might this be and how can it be restored?
Edit: I have also posted this issue to Connect.
Edit2: The following post describes the purpose of this setting.
Did you know? You can unwind the call stack from exceptions
And this one includes a couple of screenshots of the dialogs involved.
Did you know… What unwinding the call stack on unhandled exceptions does? – #277
Tools -> Options -> Debugging (General) [VS 2010]
Do you have "Unwind the call stack on unhandled exceptions" unchecked ?
It needs to be unchecked for $exception to work.
Related
While debugging in ASP.NET Core w/ VS 2019, when I have break on exceptions turned on 🟢, I break on the throwing, which is what I want 👍, but then I have to continue, F5 through each pop of the stack 🔁.
how do I say "CONTINUE out of this request & let the exception bubble up to the top w/o breaking"? is that an option in VS?
As the ASP.NET Core app is a long-running process, VS has no way of knowing where & when a request starts and ends. You can disable all breakpoints, hit F5, then re-enable them for the next request.
Use the top menu: Debug > Disable All Breakpoints
Use Breakpoints panel
You always can set a hotkey to speed up this workflow
I do believe that what you're seeing is the exception being rethrown on every async stack frame. You could confirm this if the exception dialog says that you can suppress the exception when thrown from System.Private.CoreLib.dll assembly, even though it was originally thrown in your code.
If you disable Just My Code, you should see that the exception is thrown from ExceptionDispatchInfo, which is the type used to rethrow exceptions with their original call stack in async/await. This is also what causes these exceptions to be thrown from System.Private.CoreLib.dll.
The only way I have found to improve this is to not break when the exception is thrown from System, either by using exception filter with module parameter, or in exception dialog > Exception Settings. This is only valid for exceptions that you never encounter from System.Private.CoreLib.dll. I am also not sure how this would work for Just My Code and external libraries as an exception might reach your code only after redispatch from async/await machinery.
With that in mind, you'll often just have to F5 all the way or rely more on breakpoints instead of exceptions for debugging.
Alternatively, as hinted in the other answer, you could disable exceptions until the request completes and re-enable them.
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.
I'm working on a project where a lot of bad code is written.
Today I came across a piece of code that caught and exception and just returned an empty string to "handle" it (very difficult to debug).
I was wondering whether there was any way of knowing that an exception has been thrown and caught in visual studio 2010?
VS menu -> Debug -> Exceptions -> Enable CLR Exceptions
(CTRL+ALT+E)
There you can choose from "Thrown" or "user un-handled", obviously you need to break on "Thrown" exceptions
The debugger can break execution of your application immediately when
an exception occurs, giving you a chance to debug the exception before
a handler is invoked.
More details on MSDN: How to: Break When an Exception is Thrown
Important note - this feature is not available on Visual Studio "Web Developer" edition
Go to the 'debug' menu, select 'exceptions' and check 'Thrown' next to Common Language Runtime Exceptions. When debugging, this will break at any point an exception is thrown when debugging.
On the "Debug" menu choose "Exceptions..." and then tick "Thrown" and/or "User-Unhandled" for Common Language Runtime Exceptions.
This is not possible.
Visual Studio has settings for stopping either when unhandled exceptions occur or whenever an exception is thrown (or both).
There is no setting for exceptions that have been caught (as this would be a very common case and would overwhelm the display).
The debugger writes a entry to the output window when an exception is thrown.
http://msdn.microsoft.com/en-us/library/x85tt0dd.aspx
You can break when an exception is thrown.
You can use ReSharper to detect unused parameters (catch(Exception e))
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
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
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.
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.
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:
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.)
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.
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).
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.