views:

54

answers:

4

I am debugging some code, and would like to throw an error without actually having the code in place for exception.throw.

While I am debugging and the line of code is highlighted in yellow inside a function, is there anyway to dynamically throw a general exception?

This would save me heaps of time during debugging sessions.

Thanks

A: 

What information are you hoping to see if you throw the exception yourself?

Black Frog
Just a general exception, to step into the try / catch scenario I have in place, not a specific exception in this case.
JL
A: 

My advice would be to put together a test case which guarantees, due to the input data, that the program will throw an exception at the point you are looking at.

You only need a try/catch when an exception can possibly be thrown, so there must be a throw somewhere - its just a case of triggering it.

The alternative is to provide some sort of mock object that is called at the line of code in question, who's only job is to throw the exception you require.

quamrana
+1  A: 

Are you asking if you can programmatically cause the debugger to break, if so, then:

Debugger.Break();

If you want to throw an exception at that debugged point, if you have optimised build disabled, you can edit-in-place, and insert a throw new Exception(); statement just after it, move the point of execution to that line, step over that particular line, which will cause the debugger to jump to the catch. Then you can just remove the line you just inserted. There is no way to do it without inserting some actual code to throw an exception.

Kazar
A: 

Ok, I'll answer my own question because I really think this sort of feature should be included in .net IDE's of the future.

Imagine debugging code and while stepping, and a section of code is highlighted, right click and then select throw exception. A drop down list is provided with pre-defined exceptions or else just a general new exception. This way you can test code much quicker without having to write specific test cases.

I also don't believe that writing test cases is always possible. For example, lets say I am requesting data from a web service. And I want to catch the following exceptions: SSL Trust Failure, Connection refused , 404 resource not found. I would be amazed if there are ways to write these test cases, when the service you're requesting is not your own. In these cases imagine how much better it would be if you could just step in, and at the required point, right click, select throw new exception - then via a dialog select the exact type of exception you want. . In this case you run the code once in debug mode, each time you want to test for a new exception type, just doing the right click thing. Not having to stop / start the debugger or have any temporary throw exceptions coded all over the place which will ultimately need to come out for a production release.

I'll also mention you should also be able to step back out of the catch sections, which is not currently possible as far as I know.


Anyways, as far as I know the best way to trigger an exception like this at debug time dynamically - is to drag and drop the debug location to a point where it was relying on code to run previously that never did, hence making the current line of code you drop on invalid. This is the only way I know how to do this, so if you know any other way please leave a better answer.

To illustrate my point

  1. Create Dir
  2. Create File in Dir

If you don't run line 1, by dragging and dropping the debugger onto line 2, then line2 will now throw an exception because now the directory does not exist, which would have if the code had run normally.

Lol - if I worked for VS team, you would all be much happier developers.

JL
If you want this added as a feature to future releases, then enter a request on Connect, http://connect.microsoft.com/visualstudio/. Then post the URL of your request here, so we can vote on it.
John Saunders