swiftlang / swift-book

The Swift Programming Language book
Apache License 2.0
1.76k stars 162 forks source link

Throw Statements May Be Given in Do Code Blocks #256

Open tadbyt opened 10 months ago

tadbyt commented 10 months ago

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/errorhandling#Handling-Errors-Using-Do-Catch https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements#Throw-Statement https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements#Do-Statement

Description

Per the Throw Statement subsection:

A throw statement occurs in the body of a throwing function or method, or in the body of a closure expression whose type is marked with the throws keyword.

This is only partially correct. Throw statements may also be given in the code block of a do statement that is not contained in such a function, method, or closure.

Correction

Append the following sentence to the first paragraph in the Throw Statement subsection:

A throw statement may also occur in a do statement code block.

Additionally, the form of the do statement given in both the Language Guide and Language Reference could be altered to:

do { try <#expression#> throw <#expression#> <#statements#> } catch <#pattern 1#> { <#statements#> } catch <#pattern 2#> where <#condition#> { <#statements#> } catch <#pattern 3#>, <#pattern 4#> where <#condition#> { <#statements#> } catch { <#statements#> }

The existing discussion in the Do Statement subsection already allows for a throw statement in the do statement code block, but this would provide clarity.