原文标题:《 Solidity 极简入门: 30. Try Catch 》
Original author: 0xAA
I've been relearning solidity recently to consolidate the details and also write a "Solidity Minimality Primer" for kids to use (programming geeks can find another tutorial) and update 1-3 sessions every week.
All open source code and tutorial github:github.com/AmazingAng/WTFSolidity
try-catch is a standard way of handling exceptions in almost every modern programming language, and it was added to solidity0.6. In this lecture, we will introduce how to use try-catch to handle exceptions in smart contracts.
In solidity, try-catch can only be used for external functions or calls to constructor (treated as external functions) when contracts are created. The basic syntax is as follows:
externalContract.f() is a function call of an external contract. The try module runs if the call succeeds, while the catch module runs if the call fails.
If the called function has a return value, then returns(returnType val) must be declared after the try, and the returned variable can be used in the try module; If a contract is created, the return value is the newly created contract variable.
In addition, the catch module supports catching special causes of exceptions:
We create an external contract OnlyEven and use try-catch to handle exceptions:
The OnlyEven contract contains a constructor and an onlyEven function.
The constructor takes a parameter, a, and require throws an exception if a=0. assert throws an exception when a=1. All other conditions were normal. The onlyEven function takes an argument b, and require throws an exception if b is odd.
First, define some event and state variables in the TryCatch contract:
SuccessEvent is the event that is released after a successful call. CatchEvent and CatchByte are the events that are released when an exception is thrown, corresponding to require/revert and assert exceptions, respectively. even is a state variable of the OnlyEven contract type.
We then use try-catch in execute to handle exceptions in the call to the external function onlyEven:
When execute(0) is run, no exception is thrown because 0 is even, and the call succeeds and releases the SuccessEvent event; When execute(1) is run, because 1 is even, the exception is thrown, the call fails and the CatchEvent event is released.
Here, we use try-catch to handle exceptions during contract creation. Just rewrite the try module to create the OnlyEven contract:
You can run executeNew(0), executeNew(1), executeNew(2), and see what happens.
In this lecture, we introduced how to use try-catch to handle exceptions in the running of a smart contract:
Can only be used for external contract calls and contract creation.
If the try executes successfully, the return variable must be declared and of the same type as the returned variable.
The original link
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia