错误处理-错误传播 [英] Error Handling - Error Propagation
问题描述
我有三个职能。 A --calling-> B --calling-> C.没有任何闭包参数的简单函数,例如读取plist的函数,验证...
I have three functions. A --calling--> B --calling--> C. Simple functions without any closure arguments, say functions for reading plist, validations...
C中出现错误。我在这里使用Do / Try / Catch在函数之间传递错误。
Say an error arises in C. I am using Do/Try/Catch here to pass the errors across functions.
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
}
顾名思义,我尝试过使用throw,但是需要使用throw进行封闭!
是否有其他选择?
I tried using rethrow, as the name suggests, but it needs a closure with throw! Any alternatives?
推荐答案
b()
必须是标记为抛出
,需要调用尝试c(1)
。
但是您不需要做捕获块。因此
b()
must be marked with throws
, and needs to call try c(1)
.
But you don't need a do-catch-block. So
func b(param: Int) throws {
// do something ...
try c(1)
// do more ...
}
将将在 c()
中引发的错误传播给 b()
的调用者。
will propagate an error thrown in c()
to the caller of b()
.
这篇关于错误处理-错误传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!