Jacob Carpenter’s Weblog

October 6, 2008

C# compiler eccentricity of the day: throwing lambda

Filed under: csharp — Jacob @ 4:21 pm

Here at work (gratuitous link; oh yeah, and we’re hiring), we have a Verify helper class. Verify lets you succinctly validate (or verify, if you will) various method invariants. For instance, non-nullness:


The problem with these helper methods is that when an invariant is violated, an exception is thrown far from the actual affected bit of code. Always moving one frame up the call stack to see the real code that’s failing quickly gets annoying.

I started thinking about how to mitigate this problem, and realized that with Visual Studio’s excellent debugger support for delegates, the call site could include an Action<Exception> that just threw (throw-ed?):

Verify.IsNotNull(name, ex => throw ex);

Except that doesn’t compile.

Wrap the lambda body in curly braces (don’t forget the extra semi-colon) and everything works as expected (including the debugger breaking within the lambda!):

Verify.IsNotNull(name, ex => { throw ex; });

But unfortunately that adds so much syntax that the “solution” is more annoying than the problem I was initially trying to solve.

Has anyone run into anything like this before? Does it make any sense why that statement wouldn’t be valid as a lambda expression?



  1. The brace-less format is basically a return statement, without the word “return”. (It says that somewhere in the language spec, but I don’t have the reference right now.) Since you can’t say “return throw ex”, I think that’s why you can’t say ex => throw ex.

    I wonder if you could achieve the result you want by omitting the lambda, and instead marking the IsNotNull method with DebuggerNonUserCodeAttribute. With any luck, that will cause the debugger to break at the call site, rather than in the method body. (Other possible alternatives are DebuggerStepThroughAttribute or DebuggerHiddenAttribute).

    Comment by John Rusk — October 7, 2008 @ 12:46 am

  2. Because throw is a statement, not an expression. It returns no value.

    Comment by Blake Coverett — October 7, 2008 @ 2:13 am

  3. John–

    I just had the “implicit return” discussion with a co-worker, too. But here’s my counter-example:
    Action action = () => Console.WriteLine(“Hello World”);

    Nowhere can you say “return Console.WriteLine(…)” and get it to compile.

    However, decorating the method with [DebuggerNonUserCode] does exactly what I want! Thanks.

    Comment by Jacob — October 7, 2008 @ 7:46 am

  4. I think you meant, “… the call site could include an Action that was just thrown.” You want a passive verb here because the agent of the verb (in this sentence, the thing throwing the exception) isn’t named. If, however, you say “that just threw”, you’re implying a reflexive action, as in “that just threw [itself]”. For all I know, that may be what you meant. But we can all agree that “throw-ed” is just plain silly.

    Was this comment helpful? ( )quite( )somewhat (x)meh ( )not at all ( )whiskey-tango-foxtrot!

    Comment by Eli — October 7, 2008 @ 1:46 pm

  5. >Action action = () => Console.WriteLine(”Hello World”);

    Good point!

    Glad the other solution worked.

    Comment by John Rusk — October 8, 2008 @ 12:49 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: