How to force VS2019 ignore exceptions inside "try" section - visual-studio

Just installed VS2019 and noticed one uncomfortable thing: in Debug it stops ("falls") at the each exception, even inside try sections. How can I force it to ignore these exceptions, go to catch and work further?
After answer of Perry Qian-MSFT. Now I see this when I have UNHANDLED exception.
Translation: "application is in pause mode". And call stack is empty.

How to force VS2019 ignore exceptions inside “try” section
First, I agree with dixv and thanks for his help.
Current VS IDE can ignore specific exceptions as required.Under Debug-->Windows-->Exception settings
You can record the previous exception's name and search under it and then uncheck it.
Note
After that, please uncheck Just My Code option, otherwise the exception may still be interrupted.
Tools-->Options-->Debugging-->General-->Just My Code
Update 1
If you enable that feature and also want to see the detailed info about that exception without breaking Debugging, you can add these code in catch
try
{
xxxxxxxxxxxxxxx
}
catch (Exception e) {
Debug.WriteLine("=============================");
Debug.WriteLine(e.Message);
Debug.WriteLine(e.Source);
Debug.WriteLine(e.StackTrace);
Debug.WriteLine("=============================");
}
It will show the specific info about that exception in output window:
More info, you can refer to this issue.
Update 2
When you face that situation, you can just click on View Detail of that exception window, it will call Watch Widow and then when you click on StackTrace, it will show the related error file name and related line to you.
Hope it will help you.

Appears, there're two possible ways to... not solve this issue, but at least make it less uncomfortable.
1) Update 2 from Perry Qian-MSFT
2) I enabled Diagnostic Tools while debugging and when exception appears, I just select it in the log of the DiagTools and VS shows me required line.

You probably have the debugger set to break on first chance exceptions, which means it will break when the exception is thrown regardless of any code to catch it. To prevent this behavior, uncheck the respective exception types under Debug / Windows / Exception Settings.
From Manage exceptions with the debugger in Visual Studio:
The debugger can break execution at the point where an exception is thrown, so you may examine the exception before a handler is invoked. [...]
If you select an exception in the Exception Settings window, debugger execution will break wherever the exception is thrown, no matter whether it's handled. Now the exception is called a first chance exception.

Related

Prevent Asp.net MVC WebAPI from stopping (needing to press continue) when exception is caught

I have a try and catch block in a controller in my asp.net MVC webAPI project.
This is the exception that I get if I mess up the date input
An exception of type 'System.ArgumentException' occurred in
mscorlib.dll but was not handled in user code
Additional information: An item with the same key has already been
added.
i tried to add the following
catch (Exception e)
{
output.Add("requestStatus", "fail");
output.Add("errorSumary", "=== Exception in SOAP request ===");
output.Add("errorSuggestion", "effectiveDate was not in the right format or the date already past");
logger.Error("Error: ActivateController - effectiveDate was not in the right format");
}
but when this exception is triggered I still have to press the continue button in visual studio to make this program keep running. Is there any way to make it so the exception doesnt stop the running of the program? (ie so i dont have to click the green Continue button in visual studio)
I know i should try and catch everyType of exception and handle them gracefully I just wanted a built in failsafe that allows it to keep running if one slips under the rug (and I log then using Nlog so you could see if anything is broken via something like splunk).
So it turns out in order to fix this all I had to do was change the following setting in visual studio to be checked when running in debug mode.
The setting is called
break when this exception type is user-unhandled .
I was able to confirm this by switching the Visual Studio release configuration from "debug to Release" where the program would not only catch the exception, but the program would no longer stop its execution thus continuing to catch it time and time again without interrupting the program. I didn't realize the debug configuration by default had the "break when this exception type is user-unhandled" turned on out of the box.
You can see your exception settings in two places I have found. Debug -> Exceptions (and likewise set those back to the defaults if you want to re-enable this behavior).
more information about your debugging settings can be found here
Tools->Options->Debugging

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.

Is there a setting to show me when an exception is caught?

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))

vs2010 debugger exception view

My vs2010 shows this message when an exception is thrown:
How can I configure the debugger to show exceptions like this instead:
Thanks!
Big difference between the two. You'll only see the first one when you explicitly configure the debugger to stop when an exception is thrown. You do so with Debug + Exceptions, Thrown checkbox. You don't often use this, really only when your code contains too many catch statements that swallow exceptions inappropriately and making the code misbehave. It also has a knack for showing exceptions in code you didn't write. Click the Break button to allow the debugger to show you the details.
You'll only get the second screenshot when an exception is thrown and there is no catch block to catch it, making it an unhandled exception. That's a fatal error, the program cannot continue. The debugger stops to show you the problem, it is otherwise the end of the debugging session. Without a debugger your program will crash. This is the 'good' kind, you get it by omitting try/catch blocks so your program terminates when something unexpected happens. You will want to write an event handler for the AppDomain.CurrentDomain.UnhandledException event so the user at least has an idea what went wrong. And you for that matter.

How to temporarily deactivate all try / catch blocks?

Is there a possibility to deactivate / activate all try catch blocks in the whole project as easy as clicking a button?
I need this for debugging when I don't want the catch block to handle the exception, but instead prefer that VS breaks into the code as if the try catch block was not there.
At the moment I am commenting out the try/catch blocks but this is inefficient.
Environment: VS 2008 with C# as language.
To catch exceptions the moment they're thrown ("first-chance exceptions" in Win32 parlance):
in VS2008: go to Debug, Exceptions...
by VS2015: this has been moved to Debug > Windows > Exception Settings
Then check the box Thrown for Common Language Runtime Exceptions.
There is no way to deactivate try/catch blocks.
However, for debug purposes, if you want to break as soon as a particular type of exception is thrown, you can get Visual Studio to do that (Debug -> Exceptions; select the exception type you're interested in and check the "Thrown" box).
Thought about this today and found an other solution. The advantage of this is that the IDE will stop at the exact point of the occuring exception.
Somewhere globaly defined:
Namespace Global.System
Public Class NeverOccurException
Inherits Exception
End Class
End Namespace
At the beginning of each source code file:
#If DEBUG
Imports CatchAtReleaseException = System.NeverOccurException
#Else
Imports CatchAtReleaseException = System.Exception
#End If
Usage:
'Compiled in DEBUG-Mode the TryCatch is "disabled", because the
'the ALIAS CatchAtReleaseException is set to "NeverOccurException"
'Compiled as RELEASE the TryCatch is "enabled", because the ALIAS
'is set to the regular "System.Exception"
Public Sub SampleSub()
Try
'...
Catch ex As CatchAtReleaseException
End Try
End Sub
Have fun with it,
Greetings,
Ted
If you want to do in the IDE, Debug -> Exceptions is the dialog where you can ask the IDE to break when a specific/category/all exceptions are thrown.
You can change the way Visual Studio breaks when an exception occurs. By default, it breaks on unhandled exceptions. If you go to menu Debug > Exceptions, you can uncheck Common Language Runtime Exceptions and make other changes in the IDE's behavior when exceptions occur. For example, you can have it break on only one kind of exception; there's a long list there.
I have done this on rare occasions when trying to debug.

Resources