Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw an exception during debugging session in VS2010

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.

like image 710
Clueless Avatar asked May 19 '11 11:05

Clueless


People also ask

How do I skip the code while debugging in Visual Studio?

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.

What is exception handling in debugging?

Exception handling is all about ensuring that when your program encounters an issue, it will continue to run and provide informative feedback to the end-user or program administrator.

How do I allow exceptions in Visual Studio?

With a solution open in Visual Studio, use Debug > Windows > Exception Settings to open the Exception Settings window. Provide handlers that respond to the most important exceptions.


1 Answers

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.

like image 61
Piblip Avatar answered Oct 22 '22 03:10

Piblip