How to throw an exception during debugging session in VS2010

Clueless picture Clueless · May 19, 2011 · Viewed 18.1k times · Source

I have a small issue. Sometimes when I debug an application I want to simulate during the debug session an exception thrown from a method, but there is no way to do it. I can't even drag the cursor (the yellow one that indicate the current line) to an exception block. The only way to trigger an exception during the run is to change the current line of code and to write the exception I want to throw. This method is not good because I might forget to change it back in the next run.

Answer

Piblip picture Piblip · Dec 28, 2011

The only really workable way I have found to throw exceptions from within a method during debugging is to pull variable from under the feet of the executing process. I.e. set a variable that is about to be used to null will obviously throw an exception as soon as it is executed. However this will not allow you to throw custom exceptions (e.g. specific types or messages).

For specific exceptions we need to modify code according to previous posts.