0

I have a method that is called frequently and sometimes an exception happens that I catch and handle.
But it is annoying that VS keeps breaking at that code everytime an exception happens, I would like to tell VS to just keep going since the exception is handled anyway.

So I tried using the DebuggerStepThrough attribute but when I insert it in my code I get hundreds of errors.

Is there a way to tell VS to not break on this particulair piece of code when an exception happens

In the code below I dont wont the debugger to break when an exception happens

// some code here

//[DebuggerStepThroughAttribute()]
[DebuggerStepThrough] // this does not compile
try
{
    _Table.PrimaryKey = new DataColumn[] { Table.Columns[PrimaryKeyName] };
}
catch 
{ // code for when exception happend...
}

// more code here...
GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • It's not good to intentionally throw exceptions, can you not do some validation before assignment? – Sasha Jul 27 '18 at 09:31
  • @Jaxi I am not intentionally throwing any exception. I am just catching one that might occur what is wrong with that ? – GuidoG Jul 27 '18 at 10:07
  • If the exception is preventable, it should be prevented – Sasha Jul 27 '18 at 10:41
  • @Jaxi You are missing the point here. – GuidoG Jul 27 '18 at 10:44
  • I'm missing your original point I agree, but it's still bad practise. – Sasha Jul 27 '18 at 10:47
  • 1
    https://stackoverflow.com/questions/2427370/visual-studio-ignore-try-catch-debug-only – Sasha Jul 27 '18 at 10:47
  • @Jaxi Yes I also agree but not everything can be prevented. In this case it was not possible. But lets focus on the question please – GuidoG Jul 27 '18 at 10:48
  • See the link I provided, it should do as you need. – Sasha Jul 27 '18 at 11:40
  • @Jaxi No exact, this only skips code when you are not debugging. That is not what i need. I need that VS does not breaks when this particulair exception happens – GuidoG Jul 27 '18 at 13:14
  • What version of VS do you have? Have you tried looking at the exceptions settings dialog? (Ctrl-D, E) – Chris Dunaway Jul 27 '18 at 17:51
  • @ChrisDunaway I have version 14.0. update 3. Ctrl-D, E seems to do nothing. Also note that I dont want VS to not stop on this kind of exceptions, but only not on this particular exception in this line in the code. So I was hoping for some kind of decoration or something i can put in code – GuidoG Jul 30 '18 at 06:59
  • How about use Exception Settings to ignore this exception, Debug->Windows->Exception Settings (CTRL+ALT+E). https://stackoverflow.com/questions/4499473/how-to-tell-the-debugger-to-ignore-breaking-on-thrown-exceptions – Leo Liu Jul 30 '18 at 09:08
  • @LeoLiu-MSFT I dont want to stop VS from breaking on all excepitions like that, only on this particular one, in this cs file. Is that possible then ? – GuidoG Jul 30 '18 at 09:18
  • @GuidoG, How about using DebuggerNonUserCode? https://stackoverflow.com/questions/1644780/visual-studio-debugging-ignore-exception-in-one-place-while-breaking-at-it-els – Jack Zhai Jul 31 '18 at 09:03
  • @JackZhai-MSFT Yes but only works for an entire method no ? I need only skip this exception in the method, not the entire method – GuidoG Jul 31 '18 at 10:50
  • @GuidoG, No better suggestions for this issue, or you could view the tool PostSharp with custom attribute to ignore method exception: https://helpercode.com/2011/02/07/how-to-ignore-thrown-exceptions/. Actually ignore exception is really dangerous for developing, if possible, I also suggest you really resolve the exception, anyway, if the previous suggestion was no helpful for you, you could add your suggestion to the product team here: : http://visualstudio.uservoice.com/forums/121579-visual-studio. – Jack Zhai Aug 01 '18 at 02:20

1 Answers1

0

You should be able to handle this in the exception settings tab (CTRL-D, E) or Click Debug > Windows > Exception Settings.

If you clear the checkbox under "Break when thrown", then if you have handled the exception in code, it should not break in the debugger. You can do this on an individual exception basis. If the specific exception type doesn't exist, then click the + button to add it to the list.

I have tested this under the same version you mention in your comment.

enter image description here

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
  • 1
    My understanding is that GuidoG doesn't want to disable the same Exception for the whole project. He just wants to disable one in certain specific method or code line. – Jack Zhai Aug 03 '18 at 10:07