10

I'm using exceptions to validate a control's input in Silverlight 4. When I throw an invalid input exception, VS 2010 displays the popup and stops the program. I ignore this and resume the program, and everything continues fine (since the exception is used to signal a validation error.) Is there a way to mark that one exception as ignored?

I'm following this tutorial.

5
  • 5
    Nearly an exact duplicate of: stackoverflow.com/questions/2689065/… (and my answer applies equally here). I'd advise against using exceptions for validating user input -- if anything, correct input is the exception and mistakes the rule, not the other way around. Sep 10, 2010 at 21:53
  • I know. But this appears to be the way Silverlight does it. (Or so it seems. I'm new to SL.) Sep 10, 2010 at 22:11
  • You could consider the Try* approach, like int.TryParse("123", ref result) that returns a bool indicating if the operation succeeded.
    – zneak
    Oct 3, 2010 at 17:12
  • @Rosharch is right - validation controls on Silverlight work via exceptions, which can be quite annoying unless you disable breaking on them. Aug 6, 2011 at 13:39
  • @NickHeiner If you like my answer can you please accept, I'm trying to get my profile ready and green would look nice on my developer story. Thanks! - JB Sep 24, 2019 at 15:31

7 Answers 7

19

Debug -> Exceptions -> Uncheck

1
  • 3
    Yes, but you should be able to disable it directly from the popup. Jan 24, 2014 at 11:05
5

Menu, Debugger, Exceptions...

In that dialog, you can remove the checkmark in the 'thrown' column for one exception, of for a whole namespace. You can add your own. etc.etc.

1
  • This was useful information and allowed us to create a custom exception to reset FPU registers from using an unmanaged dll and be able to debug as normal. Very handy!
    – ouflak
    Jan 7, 2015 at 13:32
5

I got [System.Diagnostics.DebuggerHidden()] to work if I also selected

Debug > Options > Debugging > General > Enable Just My Code (Managed only).

I access the Excel object model a lot, and I really like to be able to run the debugger and catching all exceptions, since my code normally is exception less. However, the Excel API throws a lot of exceptions.

// [System.Diagnostics.DebuggerNonUserCode()]  works too
[System.Diagnostics.DebuggerHidden()]
private static Excel.Range TrySpecialCells(Excel.Worksheet sheet, Excel.XlCellType cellType)
{
    try
    {
        return sheet.Cells.SpecialCells(cellType);
    }
    catch (TargetInvocationException)
    {
        return null;
    }
    catch (COMException)
    {
        return null;
    }
}
1
2

When you run Visual Studio in debug mode, there are Exception Setting on the bottom tool bar. Once you click it, there are all type exceptions. Uncheck exceptions that you want. For the Custom exception you made for this project, they are located in Common Language Runtime Exceptions, at very bottom. Hope this helpful.

0

You can disable some throw block by surrounding in the block

#if !DEBUG
       throw new Exception();
/// this code will be excepted in the debug mode but will be run in the release 
#endif
1
  • 4
    Won't this stop the exception from being thrown? I want it to be thrown, I just don't want VS to stop the application and pop up that box. Sep 10, 2010 at 22:11
0

Putting this above the property that throws the exception seems like it should work but apparently doesn't: [System.Diagnostics.DebuggerHidden()]:

    private String name;

    [System.Diagnostics.DebuggerHidden()]
    public String Name
    {
        get
        {
            return name;
        }
        set
        {
            if (String.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("Please enter a name.");
            }
        }
    }
0

From Visual Studio 2015 onward there is an Exception Settings window for this.

Debug > Windows > Exception Settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.