I am running my application in debug mode, and I would like to manually throw an exception (i.e. not from within the code). Is there any way to do this?
Of course, running throw new Exception("My forced exception");
in the Command or Immediate window doesn't work.
EDIT: I want the exception to be caught by the try-catch statement that surrounds the code I'm debugging.
You can also click on the line you want to skip to and hit Ctrl+F10 (Run to Cursor). It will jump directly to that line.
To end a debugging session in Microsoft Visual Studio, from the Debug menu, choose Stop Debugging.
One possible way is to break on a line and manually change a nullable variable in the code path to null
just before an operation on it occurs. This will cause a NullReferenceException
to be thrown.
You could add a method similar to:
public static void ThrowAnException(string message) { throw new ApplicationException(message); }
Then, using the Immediate window, you could call ThrowAnException("Whoops")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With