0

I'd like to have a function take a statement as a parameter, either as a string or as some other type that I'm not aware of, and return true if the execution of that statement throws an exception, and false otherwise.

This may rely on some sort of "string" execution, which I have no idea how to do in C#.

user420667
  • 6,552
  • 15
  • 51
  • 83
  • Can you provide an example of a statement you would use? – Bernard Feb 02 '11 at 20:59
  • What would be the purpose of doing this as opposed to, say, using a `try..catch` block? The answers given below are creative, but this is a strange-enough request that I have to question the premise. I.e. what @LBushkin said. – Dan J Feb 02 '11 at 21:00
  • **This isn't necessarily possible**. Performing semantic analysis on a statement to determine it's behavior is an extremely complex problem, and often depends on having more than just a statement in isolation to work with. Actually executing the statement to see if it throws an exception can be done, but this may result in the side-effects of the statement executing as well. Not to mention that you need to have real data available for the statement to operate on. **Why exactly are you trying to do this? Perhaps there's a different way to achieve what you're looking for?** – LBushkin Feb 02 '11 at 21:00
  • Why? What are you trying to do? – SLaks Feb 02 '11 at 21:00
  • @djacobson & SLaks: Really the only reason for this is to make it easier to test my code. I'd rather not have to encapsulate each test in a try catch block. Also, as far as I know, C# doesn't have macros, so that's out of the question as well. – user420667 Feb 02 '11 at 21:02
  • 1
    @user420667: There are _much_ better ways to test one's code. You don't have to wrap all of your tests in try/catch blocks, proper tests in a proper testing framework would simply "fail" on an exception. – David Feb 02 '11 at 21:08
  • @David: explain "proper" and why this method is bad. – user420667 Feb 02 '11 at 21:18

5 Answers5

4

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(); });
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Btw, I am using NUnit, in conjunction with this method. Assert.IsTrue(ThrowsException(()=> somethingThatshouldThrowException())))))))) – user420667 Feb 02 '11 at 21:08
  • aaaah, double win. I like the ThrowsException function better b/c this way I can test many statements without having to setup individual test methods for each one. Thank you. – user420667 Feb 02 '11 at 21:16
1

As far as I'm concerned, there is no way to do this.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

If I am understanding this correctly you want something like this:

public static bool ActionSucceeds(Action action, out Exception exception)
{
    try 
    {
        action(); 
        exception = null;
        return true;
    }
    catch (Exception ex)
    {
        exception = ex;
        return false;
    }
}
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
0

So now that we have a better context for your question...

I would suggest that for writing tests that can capture and respond to exceptions, you consider using an existing test framework, like NUnit, or MSTest.

These libraries provide the infrastructure necessary to run tests and catch exceptions (both expected and not expected) during execution. This allows an entire suite of tests to run, even if individual test fail. You can also use the Assert utilities provided to help test for failure conditions, rather than throwing exception directly.

LBushkin
  • 129,300
  • 32
  • 216
  • 265
0

You should really bubble up the exception to the code that supposedly is meant to check for the boolean value of your method in question and handle it there they way you like. Exceptions are... well for handling exceptional cases. It does not translate well into trying to return true or false from a method. Thats not what exceptions are designed to do. It puts a different connatations to your method.

dexter
  • 7,063
  • 9
  • 54
  • 71