看到此錯誤:爲什麼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
你會看到在那裏,蘋果正在做我的方式。他們的代碼錯了嗎?