Commit 9b4787f 1 parent 43f9834 commit 9b4787f Copy full SHA for 9b4787f
File tree 1 file changed +39
-9
lines changed
1 file changed +39
-9
lines changed Original file line number Diff line number Diff line change @@ -2966,21 +2966,51 @@ function callWithSignature() external {
2966
2966
##### 测验结果
2967
2967
- 100/100
2968
2968
2969
- ### 2024.10.27
2970
- #### WTF Academy Solidity 102.29
2969
+ ### 2024.10.26
2970
+ #### WTF Academy Solidity 102.30 Try Catch函数
2971
2971
2972
- ##### 笔记
2972
+ ` try-catch ` 是 ` Solidity 0.6 ` 版本中引入的异常处理机制,允许 ** (1)调用外部合约函数 ** 或 ** (2)构造函数 ** 失败时捕获异常,从而进行错误处理。
2973
2973
2974
- ##### 测验结果
2974
+ ##### ` try-catch ` 基本语法
2975
2975
2976
- ##### 测验错题
2976
+ ``` solidity
2977
+ try externalContract.f() {
2978
+ // 当调用成功时执行的代码
2979
+ } catch {
2980
+ // 当调用失败时执行的代码
2981
+ }
2982
+ ```
2977
2983
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 ` 可以捕获特定类型的异常,提供不同的处理方式:
2980
2994
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
+ ```
2982
3012
2983
3013
##### 测验结果
3014
+ - 100/100
2984
3015
2985
- ##### 测验错题
2986
3016
<!-- Content_END -->
You can’t perform that action at this time.
0 commit comments