catch e is an expression: it evaluates e and gives its value as T!, with a panic turned
into an Err. Its body runs apart from the enclosing function, so return can't jump out of it.
? and fail inside it end the catch with that error instead.
Wrong#
fn first(xs: [int]): int {
catch { return xs[0] } else 0
}Fixed#
fn first(xs: [int]): int {
catch xs[0] else 0
}Common causes#
- An early
returnwritten inside acatch { ... }block: let the block's value be the result, thenreturnafter thecatch.