gpt4 book ai didi

ios - 错误处理 - 错误传播

转载 作者:搜寻专家 更新时间:2023-11-01 06:20:49 24 4
gpt4 key购买 nike

我有三个函数。 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com