Skip to content

Commit 9b4787f

Browse files
committed
Update Note 2024-10-26
1 parent 43f9834 commit 9b4787f

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

wyeeeh.md

+39-9
Original file line numberDiff line numberDiff line change
@@ -2966,21 +2966,51 @@ function callWithSignature() external {
29662966
##### 测验结果
29672967
- 100/100
29682968

2969-
### 2024.10.27
2970-
#### WTF Academy Solidity 102.29
2969+
### 2024.10.26
2970+
#### WTF Academy Solidity 102.30 Try Catch函数
29712971

2972-
##### 笔记
2972+
`try-catch``Solidity 0.6` 版本中引入的异常处理机制,允许 **(1)调用外部合约函数****(2)构造函数** 失败时捕获异常,从而进行错误处理。
29732973

2974-
##### 测验结果
2974+
##### `try-catch`基本语法
29752975

2976-
##### 测验错题
2976+
```solidity
2977+
try externalContract.f() {
2978+
// 当调用成功时执行的代码
2979+
} catch {
2980+
// 当调用失败时执行的代码
2981+
}
2982+
```
29772983

2978-
### 2024.10.22
2979-
#### WTF Academy Solidity 102.30
2984+
有返回值时:
2985+
```solidity
2986+
try externalContract.f() returns (returnType val) {
2987+
// 当调用成功时执行的代码,并且可以使用返回的变量 val
2988+
} catch {
2989+
// 当调用失败时执行的代码
2990+
}
2991+
```
2992+
##### 捕获不同类型的异常
2993+
`catch` 可以捕获特定类型的异常,提供不同的处理方式:
29802994

2981-
##### 笔记
2995+
1. `Error`:捕获 `require``revert` 抛出的带有字符串消息的异常。
2996+
2. `Panic`:捕获 `Panic` 异常,通常由 `assert` 失败、溢出、除零等错误引起。
2997+
3. `catch (bytes memory)`:用于捕获其他异常情况。
2998+
2999+
例如:
3000+
3001+
```solidity
3002+
try externalContract.f() returns (returnType val) {
3003+
// 成功调用时执行的代码
3004+
} catch Error(string memory reason) {
3005+
// 捕获由 `require` 或 `revert("reasonString")` 抛出的异常
3006+
} catch Panic(uint errorCode) {
3007+
// 捕获由 `Panic` 引起的异常(如 assert 失败,溢出等)
3008+
} catch (bytes memory lowLevelData) {
3009+
// 捕获所有其他未匹配的异常
3010+
}
3011+
```
29823012

29833013
##### 测验结果
3014+
- 100/100
29843015

2985-
##### 测验错题
29863016
<!-- Content_END -->

0 commit comments

Comments
 (0)