How to propagate error from a function in Swift?

In order to indicate that a function, method or initializer can throw errors, we have to mention the throws  keyword after the function parameters and before the return arrow. The throws keyword indicates that the corresponding function is a error throwing function. The function propagates the error thrown inside it to the scope from which it is called. The code outside the function must handle the error by using one of the other ways such as using do…catch statement. Only functions with throw keyword can propagate errors, non-throwing functions must handle errors inside of them.

 

 

Leave a Reply