Rethrow inside the catch block

Here's one of my use cases:

    init(from decoder: Decoder) throws {
        do {
            decoded = try decoder.singleValueContainer().decode(T.self)
        } catch let error as DecodingError {
            switch error {
            case .dataCorrupted, .typeMismatch, .valueNotFound:
                decoded = nil
            case .keyNotFound:
                throw error
            }
        }
    }

What I really wanted to write was this:

do {
  // stuff
} catch DecodingError.dataCorrupted, DecodingError.typeMismatch, DecodingError.valueNotFound {
  decoded = nil
}

But the catch clause's pattern matching abilities don't seem to be as fully featured as switch.

1 Like