views:

57

answers:

2

I have something like the following:

public class FooWrapper
{
 public Action Foo { get; set; }

 public void Execute()
 {
  try
  {
   Foo.Invoke();
  }
  catch (Exception exception)
  {
          //exception is null
   //do something interesting with the exception 
  }
 }
}

When I run my unit test with something like the following:

new FooWrapper() { Foo = () => { throw new Exception("test"); } };

The exception is thrown as expected but and the catch steps through but "exception" is null. How do I get to the exception thrown by an .Invoke() to properly handle it?

+2  A: 

This sounds like a bug in the code inside your catch block. The exception value in a catch block as defined by your sample cannot ever be null. There must be a non-null exception value for that code to execute.

Can you post the contents of your catch block?

JaredPar
Boy don't I feel silly right now!Thanks everyone!!!
Mike
+2  A: 

It only ever appears null if you have a breakpoint outside the exception line; inside, it should be non-null. I've just tested it, and got an Exception with Message="test", as expected.

Marc Gravell