Search code examples
c#visual-studiovisual-studio-debugging

Is there an easier way to stop at a breakpoint only when an error happens?


I try to debug C# and use a try / catch block to put a breakpoint in the catch block so it stops at an error while you debug.

Example

try
{
     ItemData item = inventory.GetData(i)
}
catch (Exception e)
{

}

Is there an easier way to stop at an error while you debug?


Solution

  • You set breakpoint in try catch. The code in the catch will only be run when an error is encountered, otherwise it will be skipped.

    Maybe you can set conditions in breakpoint:

    1. Set breakpoint on the code.
    2. Right click and choose Conditions.
    3. Change the Conditions Expression like this: enter image description here