22

The following is an example taken from MSDN, try-catch (C# Reference)

catch (ArgumentException e) if (e.ParamName == "…")
{
}

They also say

Using catch arguments is one way to filter for the exceptions you want to handle. You can also use a predicate expression that further examines the exception to decide whether to handle it. If the predicate expression returns false, then the search for a handler continues.

Exception filters are preferable to catching and rethrowing (explained below) because filters leave the stack unharmed. If a later handler dumps the stack, you can see where the exception originally came from, rather than just the last place it was rethrown. A common use of exception filter expressions is logging. You can create a predicate function that always returns false that also outputs to a log, you can log exceptions as they go by without having to handle them and rethrow.

My code:

static bool LogFunction(System.Exception ex)
{
    System.Console.WriteLine("Writing to logfile: {0}", ex.Message);
    return false;
}

static void Main(string[] args)
{
    try
    {
        throw new System.ArgumentException("The exception message...");
    }
    catch(System.Exception ex) if (LogFunction(ex))
    {
        System.Console.WriteLine("This will not be executed, ever!");
    }
    catch(System.ArgumentException ex)
    {
        // ....
    }
}

Now, the compiler won't compile this (csc.exe), giving the following error message:

error CS1003: Syntax error, 'when' expected

Edit: The compiler raises the error for the line

error CS1003: Syntax error, 'when' expected

What am I doing wrong?

Thanks.

EDIT: I submitted feedback to MSDN pointing out the error in the documentation.

9
  • 2
    The catch filter logic is only available as part of VS2015. Is this the version you are using? Commented Nov 19, 2015 at 19:37
  • 2
    As far as I remember this if was replaced with 'when' keyword try to swap if to when Commented Nov 19, 2015 at 19:38
  • 1
    Shouldn't your if be replaced with the word when instead? Commented Nov 19, 2015 at 19:38
  • 1
    Documentation bugs for the win! Commented Nov 19, 2015 at 19:39
  • 1
    hmm. the error is also obvious ;) Commented Nov 19, 2015 at 19:42

1 Answer 1

22

That's an error on the MSDN site. As the compiler suggests, you should use when instead of if.

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

4 Comments

Is there a language spec available for a version of c# that has this feature?
Thanks, it worked! I guess someone should point it out to the MSDN guys.
@recursive It seems the latest version of the spec is for C# 5.0
@recursive Jakub is right, 5.0 was the last formal version, the 6.0 "spec" seems to be buried in the Roslyn project page in the form of roadmaps, features, and tickets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.