Visual Studio 2008, is there anyway to dynamically throw an error when debugging?
I am debugging some code and I want to make a mistake without actually having the code to throw.
While I am debugging and the line of code is highlighted in yellow inside the function, is there any dynamic throwing of a generic exception at all?
This will save me a ton of time during debugging sessions.
thanks
a source to share
Okay, I'll answer my own question because I really believe that such a feature should be included in the .net IDE of the future.
Present the debug code and during the step, and highlight the section of code, right click and select throw exception. The dropdown is provided with predefined exceptions or just as a generic new exception. This way, you can test your code faster without having to write specific test cases.
I also don't believe that writing test cases is always possible. For example, let's say I am requesting data from a web service. And I want to catch the following exceptions: SSL Trust Failure, Connection failed, 404 resource not found. I would be amazed if there are ways to write these test cases when the service you are requesting is not yours. In these cases, imagine how much better it would be if you could just go in and right-click at the right time, select a new exception, and then, in the dialog box, select the exact type of exception you want., In this case, you run code once in debug mode, every time you want to test a new type of exception, just right click. No need to stop / start debugger or have temporary exceptions coded all over the place,what ultimately should be obtained for production.
I also mentioned that you should also be able to step back from catch sections, which is not currently possible to my knowledge.
Anyway, as far as I know, the best way to throw an exception like this while debugging dynamically is to drag the debug location to the point where it relied on code that had run before, which has never been done, so the current line of code which you are lost is invalid. This is the only way I know how to do it, so if you know any other way please post a better answer.
To illustrate my point
- Create directory
- Create file in folder
If you don't run line 1 by dragging the debugger to line 2, then line2 now throws an exception because now the directory does not exist, which would be if the code worked fine.
Lol - if I was on the VS team, you would be much happier than the developers.
a source to share
You are asking if you can programmatically make the debugger break, if so:
Debugger.Break();
If you want to throw an exception at that debugged point, if you've disabled the optimized build, you can edit in-place and insert a statement throw new Exception();
right after it, move the execution point to that line, a step above that particular line that will make the debugger jump to the catch. Then you can just delete the line you just inserted. There is no way to do this without inserting any actual code to throw the exception.
a source to share
My advice would be to put together a test case that guarantees, due to the input, that the program will throw an exception at the point you are looking at.
You only need to try / catch when an exception can be thrown, so there must be a throw somewhere - this is just a case of triggering it.
An alternative is to provide some kind of mock object that is called in the line of code in question, who only the job should throw the required exception.
a source to share