top | item 45135787

(no title)

bze12 | 5 months ago

Why do we still have to write `catch let error as SystemError`? Why can't the error be inferred to have the type thrown by the function? I've always found swift's error handling syntax to be awkward

discuss

order

dep_b|5 months ago

But that's exactly what typed throws do?

`static func validate(name: String) throws(ValidationError)`

Would be handled as:

```

do {

    try UsernameValidator.validate(name: name)
} catch {

    switch error {

    case .emptyName:

        print("You've submitted an empty name!")

    case .nameTooShort(let nameLength):

        print("The submitted name is too short!")

    }
}

```

See: https://www.avanderlee.com/swift/typed-throws/

bze12|5 months ago

Oh I see, the original article didn’t use this syntax.