【问题标题】:Error Handling - Error Propagation错误处理 - 错误传播
【发布时间】:2016-01-05 19:47:05
【问题描述】:

我有三个功能。 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 的闭包! 有其他选择吗?

【问题讨论】:

    标签: ios swift error-handling


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-11-13
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 2014-10-22
      相关资源
      最近更新 更多