How does the filter parameter work on CatchBlock?

Jan 20, 2010 at 8:04 PM
Edited Jan 20, 2010 at 8:06 PM

I've realized that I need the ability to filter on a specific exception type in my Try/Catch blocks in the expression tree, like C# does:

try {

} catch(ArgumentNullException exc) {

}

To only catch a specific type of exception, I've deducted (hopefully correctly) that what I need to use is the fourth filter-parameter of the Expression.MakeCatchBlock(Type type, ParameterExpression variable, Expression body, Expression filter) factory function. I've failed to figure out exactly how the filter-parameter works though, I'm assuming it would check the type of the second parameter variable? I've tried with Expression.TypeEqual(tmp, typeof(MySpecialException)); (where tmp is the ParameterExpression i pass in for the variable-parameter to the Expression.MakeCatchBlock() function) but I can't seem to get it to work. I'm guessing I'm just missing something obvious here and just need a nudge in the right direction.

Regards,
Fredrik

Jan 20, 2010 at 8:23 PM
Edited Jan 20, 2010 at 8:23 PM

Also figured that I'd add that when I do something like this:

                var catchBlock = Et.Catch(
                    catchParam,
                    catchBody,
                    Et.TypeIs(catchParam, typeof(MyRuntimeError))
                );

I get an exception saying "The requested operation is invalid for DynamicMethod" on the top level lambda wrapping the entire compilation unit. And when I remove it, everything works fine. I assume that one solution would be to wrap the catchBody block in something like this:

                Et.IfThenElse(
                    Et.TypeIs(catchParam, typeof(MyRuntimeError)),
                    catchBody,
                    Et.Throw(catchParam)
                )

But I have a feeling this is not the way to do it :)

Jan 20, 2010 at 8:56 PM

The filter parameter corresponds to the CLR's "exception filter" functionality, as exposed by VB.NET (though not C#). You can find a description of it at http://blogs.msdn.com/clrteam/archive/2009/08/25/the-good-and-the-bad-of-exception-filters.aspx. If you want just a normal C#-style catch handler, you do not need to use the filter parameter, and the Catch overloads with only 2 arguments are sufficient. The type of the ParameterExpression will be the type of the caught exception.

Coordinator
Jan 20, 2010 at 9:01 PM

You actually want:

Expression.Try(…, Expression.Catch(typeof(ArgumentNullException), …));

The exception filters which are not supported by C# but VB.Net exposes them as “catch ex as ArgumentException when Condition”.  They also don’t work in DynamicMethods – only when you’re compiling into a method builder (due to a CLR limitation).

From: fholm [mailto:notifications@codeplex.com]
Sent: Wednesday, January 20, 2010 12:05 PM
To: Dino Viehland
Subject: How does the filter parameter work on CatchBlock? [dlr:81481]

From: fholm

I've realized that I need the ability to filter on a specific exception type in my Try/Catch blocks in the expression tree, like C# does:

try {
 
} catch(ArgumentNullException exc) {
 
}

To only catch a specific type of exception, I've deducted (hopefully I'm right) that what I need to use is the fourth filter-parameter of the Expression.MakeCatchBlock(Type type, ParameterExpression variable, Expression body, Expression filter) factory function. I've fail to figure out exactly how the filter-parameter works though, I'm assuming it's would check the type of the second parameter variable? I've tried with Expression.TypeEqual(tmp, typeof(MySpecialException)); (where tmp is the ParameterExpression i pass in for the variable-parameter to the Expression.MakeCatchBlock() function) but I can't seem to get it to work. I'm guessing I'm just missing something obvious here and just need a nudge in the right direction.

Regards,
Fredrik

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jan 21, 2010 at 7:37 AM

Ah! Figured I missed something obvious, thanks to both of you :)