# Improve the syntax of rethrows to allow to indicate which throwing arguments should be considered for the rethrows functionality

**URL:** <https://forums.swift.org/t/improve-the-syntax-of-rethrows-to-allow-to-indicate-which-throwing-arguments-should-be-considered-for-the-rethrows-functionality/50031>\
**Category:** Pitches\
**Tags:** rethrows\
**Created:** [June 26, 2021, 4:36pm UTC](https://forums.swift.org/t/improve-the-syntax-of-rethrows-to-allow-to-indicate-which-throwing-arguments-should-be-considered-for-the-rethrows-functionality/50031 "2021-06-26T16:36:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cknns](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@cknns](https://forums.swift.org/u/cknns)\
**Post date:** [June 26, 2021, 4:36pm UTC](https://forums.swift.org/t/improve-the-syntax-of-rethrows-to-allow-to-indicate-which-throwing-arguments-should-be-considered-for-the-rethrows-functionality/50031/1 "2021-06-26T16:36:17Z")

</div>

If a method is declared with the `rethrows` keyword then all the closures that throw are considered. There is no way to select only a subset of the throwing arguments.

This proposal extends the `rethrows` syntax to allow to select which of the throwing arguments should be consider by the `rethrows`.  
The new syntax will include after the `rethrows` comma separated indexes of the position of the throwing arguments that should be consider for the `rethrows`. For example:

```swift
// The method will throw only if the first closure argument throws
func method(_ aClosure: () throws -> (), aOtherClosure: () throws -> ()) rethrows(0) 

// The method will throw if the first or second closure argument throws
func method(_ aClosure: () throws -> (), aOtherClosure: () throws -> ()) rethrows(0, 1) 

//The above is equivalent with the default behaviour
func method(_ aClosure: () throws -> (), aOtherClosure: () throws -> ()) rethrows

```

**An example of a practical usecase**  
[I want to improve the XCTAssertThrowsError](https://forums.swift.org/t/change-xctassertthrowserror-function-signature-to-rethrow-when-the-error-handler-throws/45713) so that when the error handler throws then the XCTAssertThrowsError also throws.  
But If I declare the XCTAssertThrowsError with rethrows then it will also throw if the expression closure `throws` (that is something we do not want since this is error should be handled internally).  
With this improvement we should be able to write:

```swift
public func XCTAssertThrowsError<T>(_ expression: @autoclosure () throws -> T, 
                                    _ message: @autoclosure () -> String = "", 
                                    file: StaticString = #file, 
                                    line: UInt = #line,
                                    _ errorHandler: (_ error: Swift.Error) throws -> Void = { _ in }) rethrows(0)

```

_Current workaround_

One workaround you can currently use the have the same effect is to create a second method that rethrows and overloads the first method by declaring the arguments you want to be consider for the rethrow to throw.  
For example:

```swift
func foo(f: () throws -> () = {}, g: () -> () = {}) {
}

func foo(f: () throws -> () = {}, g: () throws -> () = {}) rethrows {
}

//The above two method will have the same effect as:
func foo(f: () throws -> () = {}, g: () throws -> () = {}) rethrows(1) {
}

```
