Advanced Error Handling

Welcome to Lesson 3 of Advanced Swift. In chapter 1, you’ve learned the native way to send and handle error using the error handling syntax including try, catch, and do. What if I told you, there is a function whose parameter also throws an error? Let’s take a look.

Problem

What is rethrow?

Worth Learning

Let us find out whether Swift engineers use rethrow in the first place. option-click on Array. Scroll through.

  1. Array<String>()

There are a number of functions that incorporate rethrow. But, many of us may not have realized. You will find out the why.

Design Functions

First, let us design two functions.

  1. func nonThrowableFunc() {
  2. print("From nonThrowable function")
  3. }
  4. func throwableFunc() throws {
  5. print("From throwable function")
  6. throw TestingError.randomError
  7. }

Design Error Case

Create an enum called, TestingError. It contains a case, randomError.

  1. enum TestingError: Error {
  2. case randomError
  3. }

Create Throwable Function

Create a function, rethrowableFuncWithErrorHandling. It takes a parameter whose type is () throws -> (). Execute the parameter within the function block and use do-try-catch for error handling. At the end, the function is marked with “rethrows”.

Notes: Just bear with me for 30s.

  1. func rethrowableFuncWithErrorHandling(function: () throws -> ()) rethrows {
  2. do {
  3. try function()
  4. } catch TestingError.randomError {
  5. print("Random Error thrown by the function in the parameter")
  6. } catch {
  7. fatalError("FATAL ERROR: Weird Error")
  8. }
  9. }

Pass Throwable Function

Let us pass a throwable function to rethrowableFuncWithErrorHandling.

  1. try? rethrowableFuncWithErrorHandling(function: throwableFunc)

Important: You can only use try within a do-catch block. Playground is messed up.

Pass Non-throwable Function

You may also pass a non-throwable function to rethrowableFuncWithErrorHandling.

  1. rethrowableFuncWithErrorHandling(function: nonThrowableFunc)

Important: Since the function, rethrowableFuncWithErrorHandling.is marked with rethrows, you may also enter a non-throwable function even though the parameter requires a throwable function. This is the why we, developers, do not even question the existence of the rethrows keyword.

Source Code

8003_advanced_error_handling

Conclusion

Congratulations, you’ve mastered the Swift Error Handling. To recap, you may add the rethrows keyword to a function whose parameter also can throws an error. However, you may still pass a non-throwable function since the original is identified as rethrows. This is the reason why we developers use APIs created Apple engineering without realizing that the original function may also throw an error.

In the following lesson, you will finally understand the statement, “Swift is a protocol oriented language”.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.