内部函数捕获的Swift pass错误

时间:2017-06-20 10:08:31

标签: swift

有一个函数抛出自定义错误。

enum MyError: Error {
    case someError(reason: String)
}

func someMethod() throws {
    throw someError(reason: "This is test")
}

此方法someMethod()在另一种方法中调用。我想处理这种方法之外的错误。

func anotherMethod() throws {
    do {
        try someMethod()
    } catch MyError.someError(let reason) {
        throw MyError.someError(reason: reason) // This should be handled outside this method.
    }
}

我想这样写,但这不能编译。

catch let error as MyError.someError {
    throw error
}

有没有更好的方法再次抛出相同的错误?

1 个答案:

答案 0 :(得分:0)

只需写下

func anotherMethod() throws {
    try someMethod()
}

然后错误将被移交。