statements Swift 5.3 beta
This commit is contained in:
@ -577,10 +577,18 @@ do {
|
||||
statements
|
||||
} catch pattern 2 where condition {
|
||||
statements
|
||||
} catch pattern 3, pattern 4 where condition {
|
||||
statements
|
||||
} catch {
|
||||
statements
|
||||
}
|
||||
```
|
||||
|
||||
如同 `switch` 语句,编译器会判断 `catch` 子句是否有遗漏。如果 `catch` 子句没有遗漏,则认为错误已被处理。否则,错误会自动传递到外围作用域,被某个 `catch` 子句处理掉或者被用 `throws` 关键字声明的抛出函数继续向外抛出。
|
||||
如果 `do` 代码块中的任何语句抛出了错误,程序会跳转到第一个能模式匹配该错误的 `catch` 子句。如果没有任何子句匹配,错误会传递到外层作作用域。如果错误在最顶层依旧没有被处理,程序执行会因为运行时错误而停止。
|
||||
|
||||
如同 `switch` 语句,编译器会判断 `catch` 子句是否有遗漏。如果 `catch` 子句没有遗漏,则认为错误已被处理。否则,错误会自动传递到外层作用域,被某个 `catch` 子句处理掉或者被用 `throws` 关键字声明的抛出函数继续向外抛出。
|
||||
|
||||
拥有多个模式匹配的 `catch` 子句只需其中一个匹配到错误即可。如果 `catch` 子句拥有多个模式匹配,所有的模式必须包含相同的绑定常量或变量,并且每个 `catch` 子句里所有绑定的变量或常量的类型必须相同。
|
||||
|
||||
为了确保错误已经被处理,可以让 `catch` 子句使用匹配所有错误的模式,如通配符模式(`_`)。如果一个 `catch` 子句不指定一种具体模式,`catch` 子句会匹配任何错误,并绑定到名为 `error` 的局部常量。有关在 `catch` 子句中使用模式的更多信息,请参阅 [模式](./08_Patterns.md)。
|
||||
|
||||
@ -600,7 +608,12 @@ do {
|
||||
|
||||
#### catch-clause {#catch-clause}
|
||||
> *catch 子句* → **catch** [模式](./08_Patterns.md#pattern)<sub>可选</sub> [where 子句](#where-clause)<sub>可选</sub> [代码块](05-Declarations.md#code-block)
|
||||
>
|
||||
|
||||
#### catch-pattern-list{#catch-pattern-list}
|
||||
> *catch 模式列表* → [catch 模式](#catch-pattern) | [catch 模式](#catch-pattern) ,[catch 模式列表](#catch-pattern-list)
|
||||
|
||||
#### catch-pattern{#catch-pattern}
|
||||
> *catch 模式* → [模式](./08_Patterns.md#pattern) [where 子句](./05-Statements.md#where-clause)<sub>可选</sub>
|
||||
|
||||
## 编译器控制语句 {#compiler-control-statements}
|
||||
编译器控制语句允许程序改变编译器的行为。Swift 有三种编译器控制语句:条件编译语句、线路控制语句和编译时诊断语句。
|
||||
@ -789,13 +802,11 @@ statements to compile if both compilation conditions are false
|
||||
|
||||
行控制语句形式如下:
|
||||
|
||||
> \#sourceLocation(file: `filename` , line:`line number`)
|
||||
>
|
||||
> #sourceLocation(file: file path, line: line number)
|
||||
>
|
||||
> #sourceLocation()
|
||||
|
||||
> \#sourceLocation()
|
||||
>
|
||||
|
||||
第一种的行控制语句会改变该语句之后的代码中的字面量表达式 `#line` 和 `#file` 所表示的值。`行号` 是一个大于 0 的整形字面量,会改变 `#line` 表达式的值。`文件名` 是一个字符串字面量,会改变 `#file` 表达式的值。
|
||||
第一种的行控制语句会改变该语句之后的代码中的字面量表达式 `#line`、 `#file` 和 `#filePath` 所表示的值,从行控制语句里行号的代码开始。`行号` 是一个大于 0 的整形字面量,会改变 `#line` 表达式的值。`文件名` 是一个字符串字面量,会改变 `#file` 和 `#filePath` 表达式的值。指定的字符串会变成 `#filePath` 的值,且字符串最后的路径部分会变成 `#file` 的值。
|
||||
|
||||
第二种的行控制语句,`#sourceLocation()`,会将源代码的定位信息重置回默认的行号和文件名。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user