Clueless
Clueless

Reputation: 1200

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.

Upvotes: 19

Views: 19870

Answers (6)

Christopher
Christopher

Reputation: 10647

If you're reading this answer, Visual Studio 2013 is available and supports Edit and Continue in 64-bit applications. If you have Visual Studio 2013 and are just looking for a way to throw exceptions here and there to see how your exception handling or error pages work, just use Edit and Continue and type directly into your code.

Click Debug > Start Debugging. Your web app launches. Place a breakpoint. Click through your app until the breakpoint hits. You are now free to add throw statements right into your code! Just enter throw new Exception("test") or whatever, save the file, and click continue (advance your debugger) and it will process the line you just added as if you had compiled it.

Upvotes: 0

Piblip
Piblip

Reputation: 439

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.

Upvotes: 33

George Duckett
George Duckett

Reputation: 32448

You could use conditional compilation:

#if DEBUG
    throw new Exception("Test");
#endif

EDIT: With an extra conditional:

#if DEBUG && ENABLETESTEXCEPTION
    throw new Exception("Test")
#endif

You could create a custom configuration (similar to Release/Debug) with the above conditional defined (project properties->Build->Conditional Compilation Symbols).

Upvotes: 17

Jacob Seleznev
Jacob Seleznev

Reputation: 8151

You could use

bool toThrow = false;
if(toThrow) 
{
     throw new Exception("Test");
}  

and change throw to true. Or create a method/property and call them.

Upvotes: 3

Brian Dishaw
Brian Dishaw

Reputation: 5825

Are you doing this to test that you are handling the exception in calling code?

If so you might want to look into writing some unit tests and utilize a mocking framework to pull tihs off.

I have experience with Moq (Mock) http://code.google.com/p/moq/

There are some tutorials on the site to setup and run with it, it's pretty easy once you get going.

You'll need to setup a test project in your solution first, add the Moq dll's and then write a test method that sets up the Moq of your object and tells it to throw an exception.

It'll look something like this in order to have the method on your object throw an exception.

[TestMethod]
[ExpectedException( typeof( InvalidOperationException ) )]
public void YourMethod_ThrowsIOException()
{
    var mock = new Moq<YourClass>();
    mock.Setup( obj => obj.YourMethod( It.IsAny<string>() ) ).Throws<InvalidOperationException>();

    YouClass mockedClass = mock.Object;

    mockedClass.YourMethod( "anything" );
}

I hope this helps.

Upvotes: 2

abatishchev
abatishchev

Reputation: 100348

Unfortunately, Visual Studio still doesn't support throwing an exception from Immediate Window.

As well as doesn't support lambda expressions, anonymous types, collection initializers, etc. Something like .NET 2.0 only.


Also you can use Debug.Assert(false);

Upvotes: 14

Related Questions