【发布时间】:2016-02-01 14:19:12
【问题描述】:
看到这个错误:
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")
}
}
但我的catches 涵盖了所有可能性。为什么 Swift 不“深入”分析所有可能性并告诉我没有错?
例如,在此处搜索“catch VendingMachineError.InvalidSelection”:https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508
您会在那里看到 Apple 正在按照我的方式做事。他们的代码错了吗?
【问题讨论】:
标签: swift