方法抛出异常

错误处理

enum Exception: Error {
    case case1, case2
}

//只有带关键字 "throws" 的方法才能抛出错误
func methodWithException1() throws {
    throw Exception.case1
}

//在 6 版本中:
//可以指定错误类型
func methodWithException2() throws(Exception) {
    throw Exception.case2
}

do {
    try methodWithException1()
}
catch let e as Exception {
    print("Error happened: \(e)")
}
catch {}

//在 6 版本中:
do {
    try methodWithException2()
}
catch let e {
    print("Error happened: \(e)")
}