Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A rethrowing method or function in Swift, designated by the rethrows declaration modifier, is a higher-order function that propagates errors exclusively from its throwing closure parameters. It guarantees to the compiler that the function itself will only throw an error if at least one of the closures passed to it as an argument throws an error.

Syntax

func performAction(closure: () throws -> Void) rethrows {
    try closure()
}

Call Site Evaluation

The Swift compiler evaluates rethrows statically at the call site based on the type signature of the provided closure:
  • Non-Throwing Argument: If the passed closure does not throw, the compiler treats the rethrowing function as a non-throwing function. The try keyword is not required at the call site.
  • Throwing Argument: If the passed closure can throw, the compiler treats the rethrowing function as a throwing function. The call site must be prefixed with try, try?, or try!:
    • try requires the error to be handled within the calling scope (e.g., via a do-catch block or by propagating it further).
    • try? handles the error inline by returning an Optional nil if an error occurs, bypassing the need for scope-level handling.
    • try! intentionally bypasses error handling entirely, forcing the execution and crashing the program if an error is thrown.
enum CustomError: Error {
    case operationFailed
    case transformedError
}

// Non-throwing closure passed: No 'try' needed
performAction {
    print("Safe execution")
}

// Throwing closure passed: 'try' is required
do {
    try performAction {
        throw CustomError.operationFailed
    }
} catch {
    print(error)
}

Architectural Constraints

  1. Parameter Requirement: A function declared with rethrows must accept at least one throwing parameter (typically a closure marked with throws).
  2. Internal Error Generation: A rethrowing function cannot synthesize and throw its own independent errors. It is strictly prohibited from using the throw keyword to throw an error that does not originate from its throwing parameters.
  3. Error Transformation: A rethrowing function is permitted to catch an error emitted by its throwing parameter and throw a different error in its place. The compiler recognizes this as satisfying the rethrows contract because the initial failure originated from the parameter.
func transformError(closure: () throws -> Void) rethrows {
    do {
        try closure()
    } catch {
        // Permitted: Throwing a new error strictly in response to the closure's error
        throw CustomError.transformedError
    }
}

Protocol Conformance and Overriding

In Swift, rethrows is a declaration modifier, not a distinct function type. Referencing a rethrows function as a value yields a standard throws function type. However, at the declaration level, the compiler enforces specific inheritance, overriding, and protocol conformance rules based on error-throwing capabilities:
  • A rethrows method declaration can satisfy a protocol requirement for a throws method.
  • A non-throwing method declaration can satisfy a protocol requirement for a rethrows method.
  • A throws method declaration cannot satisfy a protocol requirement for a rethrows method.
  • A method overriding a rethrows method can be declared as either rethrows or non-throwing, but cannot be declared as throws.
Master Swift with Deep Grasping Methodology!Learn More