How do I propagate errors with rethrows?

In Swift, the `rethrows` keyword is used with functions that can throw errors but only if a provided closure does. This allows you to propagate errors without having to explicitly declare the function itself as throwing unless the closure does.

When you use `rethrows`, it simplifies error handling in cases where you want to execute a potentially throwing function, while only propagating the errors that arise from that closure.

Example:

func performOperation(_ operation: (Int) throws -> Int) rethrows -> Int {
            let value = 10
            return try operation(value)
        }

        // Closure that can throw an error
        let throwingClosure: (Int) throws -> Int = { number in
            if number < 0 {
                throw NSError(domain: "NegativeNumberError", code: 1, userInfo: nil)
            }
            return number * 2
        }

        do {
            let result = try performOperation(throwingClosure)
            print(result) // Will print 20
        } catch {
            print("An error occurred: \(error)")
        }
        

Swift rethrows error propagation closure error handling function throwing functions