views:

133

answers:

2

Hi.

We are replacing the exception handling system in our app in order to conform to Vista certification, but the problem is how to force certain exceptions to be thrown, so that we can check the response.

Unfortunately the whole app was written without taking into consideration proper layering, abstraction or isolation principles, and within the timeframe introducing mocking and unit testing is out of the question :(

My idea is to introduce code which will throw a particular exception, either through a compiler directive or by respecting a value in the config file. We can then just run the app as normal and manually check how the exception is handled.

Just thought I'd put it out there and see if the SO community can think of anything better!

Cheers

+1  A: 

Introduce this code:

throw new Exception("test");

If you need the exception to always be there (i.e., not just test code), then hide it behind a command-line parameter.

C:\Users\Dude> myapp.exe /x
Ben Collins
My current thinking is:#if DEBUG if (ConfigurationManager.AppSettings["ForceExceptionA"] == "true") { throw new ExceptionA(); }#endifwhich is similar to the approach you describe.
Duncan
A: 

I might not have a clue about this but my first thought was to use aspect oriented programming to throw exceptions when certain code is run. spring.net has support for this, though I don't know how well it works for your purpose. I wouldn't take my word on this but it's something to look into.

Morten Christiansen