【发布时间】:2018-11-28 07:29:36
【问题描述】:
我有以下要求
enum CustomError1: Error {
case errorA
}
enum CustomError2: Error {
case errorA
}
public func func1(completion: @escaping () -> Void) throws {
//some code
if #somecondition {
throw CustomError1.errorA
}
completion()
}
public func func2(completion: @escaping () -> Void) throws {
//some code
if #somecondition {
throw CustomError2.errorA
}
completion()
}
func result() {
do {
try func1() {
try self.func2 (){
}
}
} catch {
}
}
结果函数给出如下错误
Invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> Void'
那是因为 func1 和 func2 给出了不同类型的错误。
因此,我需要在第一个闭包内写另一个do catch,如下所示
func result() {
do {
try func1() {
do {
try self.func2 (){
}
} catch {
}
}
} catch {
}
}
有没有办法简化这种嵌套的try catchs
【问题讨论】:
标签: ios swift error-handling