ios - 错误处理 - 错误传播

标签 ios swift error-handling

我有三个函数。 A --calling--> B --calling--> C. 没有任何闭包参数的简单函数,例如用于读取 plist、验证的函数...

假设 C 中出现错误。我在这里使用 Do/Try/Catch 来跨函数传递错误。

static func a(param: int)
{
    do
    {
       try b()
    }
    catch
    {

    }
} 

static func b(param: int)
{
     //specific tasks in func b
    c(1) //CAN I PASS THE ERROR TO FUNC a() without do/try/catch block?
}

static func c(param : int) throws
{
     //Error created and throw’ed
}

顾名思义,我尝试使用 rethrow,但它需要一个带 throw 的闭包! 还有其他选择吗?

最佳答案

b()必须用throws标记,需要调用try c(1)。 但是您不需要 do-catch-block。所以

func b(param: Int) throws {
    // do something ...
    try c(1)
    // do more ...
}

会将 c() 中抛出的错误传播给 b() 的调用者。

关于ios - 错误处理 - 错误传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620024/

相关文章:

ios - 如何监控我的 iPhone URL 访问?

ios - 如何在 didFinishLaunchingWithOptions AppDelegate 中使用框架函数?

ios - 如何获取 iOS 应用的加载地址?

android - AbsListView unregisterIRListener() 被调用

iphone - AppStore 中忽略的 UIPrerenderedIcon 设置

swift - 层变换矩阵(CATransform3D 等)但带有 IBDesignable?

ios - 调用什么函数来撤消在 UITextField 中所做的更改?

ios - 如何在 iOS 的不同语言环境中格式化日期范围(只有一年字符串)?

Javascript:处理 GET 错误 (document.createElement)

actionscript-3 - 错误1009处理