I just noticed something that I’m sure has existed for a long time, but I can't find official word. It seems I don’t need to put try in front of function calls when passed as arguments to a method with try in front of it?
func foo(_:Int) throws
func bar(_:Int)
func getValue() throws -> Int
try foo(getValue()) // OK
bar(try getValue()) // OK
try bar(getValue()) // OK
bar(getValue())
^ Error Call can throw but is not marked with 'try'
The documentation doesn’t seem to explicitly state this, as far as I can see.
A try expression consists of the try operator followed by an expression that can throw an error.
Since foo(getValue()) is an expression, a single try operator can cover both the call to foo and the call to getValue.
Although it doesn't apply to this particular expression, it's also useful to know that try has lower precedence than all infix operators:
When the expression on the left-hand side of an infix operator is marked with try, try?, or try!, that operator applies to the whole infix expression. That said, you can use parentheses to be explicit about the scope of the operator’s application.
and
A try expression can’t appear on the right-hand side of an infix operator, unless the infix operator is the assignment operator or the try expression is enclosed in parentheses.