vivin
vivin

Reputation: 1002

Error Handling - Error Propagation

I have three functions. A --calling--> B --calling--> C. Simple functions without any closure arguments, say functions for reading plist, validations...

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
}

I tried using rethrow, as the name suggests, but it needs a closure with throw! Any alternatives?

Upvotes: 1

Views: 128

Answers (1)

Martin R
Martin R

Reputation: 539965

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 ...
}

will propagate an error thrown in c() to the caller of b().

Upvotes: 4

Related Questions