swifterror-handlingthrow

does throwing an error inside a do block leads to the catch block?


I have a do catch block with a function inside that can throw. In the catch block I throw a specific error. I'm interested in what happens when the function throws so I threw an error instead of the function call. I expected this error to be thrown outside the function as it can throw but it seems like throwing errors inside a do block calls the catch block which I think makes sense. Could you confirm this behavior ?

Here is the code :

func download() throws {
    do {
        // try throwingFunc()
        throw Constants.Errors.API.invalidResponse(statusCode: 2)
    } catch {
        print("unexpected")
        throw Constants.Errors.API.invalidData
    }
}

Solution

  • as vadian confirmed a thrown error in a do scope is being caught.