Alexander Stalt
Alexander Stalt

Reputation: 977

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 ?

Upvotes: 9

Views: 7441

Answers (4)

Sam
Sam

Reputation: 1376

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.

Upvotes: 0

Jason Williams
Jason Williams

Reputation: 57892

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

alt text

(Note: This won't get the debugger to break whenever you execute a try block, only if the exception is actually thrown)

Upvotes: 18

sbi
sbi

Reputation: 224039

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.

Upvotes: 0

Flexo
Flexo

Reputation: 2556

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

Upvotes: 0

Related Questions