`catch as SomeError`

This is pretty minor, but I think we can improve the ergonomics of writing catch blocks for different types of errors.

Given a throwing function and an error type SomeError, each of the following will catch an error of type SomeError:

catch let error as SomeError {}

catch where error is SomeError {}

catch is SomeError {}

The first one (with “let”) explicitly binds the name error, and a different name could be used instead.

The second one (with “where”) uses the implicit name error, just like “catch {}” does.

However, the third one (“catch is SomeError”) does not bind any name at all. That means we cannot write, eg, “print(error)” in this block, as we could in the first two. Furthermore, the wording “catch is SomeError” seems a little off.

It would be nice if we could write “catch as SomeError”, and get the implicit name error. From a conceptual standpoint, the idea is that if a catch clause does not explicitly bind a name, then the name “error” should be implicitly bound for it.

What do you think?

6 Likes