Really the only reason for this is to make it easier to test my code.
It would be better to use a unit test framework, such as NUnit, or Visual Studio Team Test:
To say that a method should throw an exception you add the ExpectedException
attribute, for example:
[Test]
[ExpectedException(typeof(ArgumentException)]
public void NullUserIdInConstructor()
{
LogonInfo logonInfo = new LogonInfo(null, "P@ss0word");
}
If you don't add this attribute and the method throws then the test automatically fails.
For completeness I'll also answer the question you asked: you can't execute a string but you can pass an Action as a parameter.
bool ThrowsException(Action action)
{
try
{
action();
return false;
}
catch
{
return true;
}
}
You can use it like this, for example:
bool result = ThrowsException(() => { throw new NotImplementedException(); });