看到此错误:为什么Swift看起来“深入”我的错误处理?
enum MyError: ErrorType {
case Foo
case Bar
}
func couldThrow(string: String) throws {
if string == "foo" {
throw MyError.Foo
} else if string == "bar" {
throw MyError.Bar
}
}
func asdf() {
do {
//Error: Errors thrown from here are not handled
//because the enclosing catch is not exhaustive.
try couldThrow("foo")
} catch MyError.Foo {
print("foo")
} catch MyError.Bar {
print("bar")
}
}
然而,我catch
ES涵盖所有的可能性。为什么Swift不会“深入”地分析所有可能性并告诉我什么是错的?
例如,搜索“抓VendingMachineError.InvalidSelection”在这里:https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508
你会看到在那里,苹果正在做我的方式。他们的代码错了吗?