0

Which factory method in System.Linq.Expressions.Expression should I call to create an expression tree -- more specifically, a CatchBlock instance -- which represents the catch in the following C# code:

try { // ... } catch { // ... } 

All the overloads of the Catch method seem to require either an exception type -- the equivalent of this:

catch (Exception) { // ... } 

and/or a ParameterExpression which will be bound to the exception -- the equivalent of this:

catch (Exception ex) { // ... } 

Passing null into the first argument (and casting to Type to avoid ambiguity):

// using static System.Linq.Expressions.Expression Catch((Type)null, Constant(true)); 

causes an ArgumentNullException.

The MakeCatchBlock method has the same behavior

15
  • 1
    AFAIK (and according to this post as well), try {...} catch {...} is equivalent to try {...} catch(Exception) {...} - so you can simply create a catch(Exception) {...} expression instead of catch {...} expression. Commented Mar 31, 2019 at 13:11
  • 1
    @CamiloTerevinto It seems that a plain catch is a valid way of handling non-CLS exceptions, which may not inherit from System.Exception. Commented Mar 31, 2019 at 13:27
  • 1
    @ZoharPeled The documentation indicates otherwise -- a plain catch will handle a non-CLS exception. Commented Mar 31, 2019 at 13:29
  • 1
    @CamiloTerevinto That answer has no references in support of its' claims, and the documentation page I linked to has been updated a number of times since .NET 3.0. This answer from the same question references the documentation page. Commented Mar 31, 2019 at 13:35
  • 1
    @ZoharPeled Did you read past the first bullet point? In C# you cannot throw non-CLS exceptions, but you can catch them in two ways: ... * Within a general catch block (a catch block without an exception type specified) that is put after all other catch blocks. Commented Mar 31, 2019 at 13:40

1 Answer 1

1

The usage of try {...} catch {...} to catch exceptions thrown from non .Net components and therefor don't inherit from System.Exception is misguided since the CLR automatically wraps such exceptions with a RuntimeWrappedException which obviously does inherit System.Exception - and therefor you can use try {...} catch(Exception e) {...} to catch thous exceptions as well.

Therefor, there is no need to handle the plain try {...} catch {...} separately from try {...} catch (Exception e) {...} since they both will catch all exceptions.

Sign up to request clarification or add additional context in comments.

2 Comments

there really is no point of using try {...} catch {...} -- saving an extra variable declaration? But I was thinking the answer should be, since the two are equivalent, there is no need to handle the plain try {...} catch {...} separately from try {...} catch (Exception e) {...}. see here.
rephrased as requested.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.