In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is Solidity error handling". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is error handling?
Error handling refers to the way in which errors are handled when errors occur in the program. Solidity handles errors differently from our common language. Solidity handles errors by backing back the state. When an exception occurs, the state changed by the current call (and all its child calls) is undone and an error identity is returned to the caller. Note that it is impossible to catch exceptions, so there is no try... Catch... .
Why is Solidity handling errors designed this way? We can think of the blockchain as a globally shared distributed transactional database. Global sharing means that everyone involved in the network can read and write the records. If you want to modify the contents of this database, you must create a transaction, which means that the changes to be made (if we want to change both values at the same time) can only be fully applied or not done at all. Students who have studied the database should understand the meaning of transactions. if you do not quite understand the word transaction, it is recommended that you search for "database transaction". Solidity error handling is to ensure that each call is transactional.
How to deal with it
Solidity provides two functions, assert and require, to check the condition and throw an exception if the condition is not met. The assert function is usually used to check (test) internal errors, while the require function is used to check whether the input variable or contract status variable meets the conditions and to verify the return value of the external contract called. In addition, if we use assert correctly, a Solidity analysis tool can help us analyze the errors in the smart contract and help us find the bug with logic errors in the contract.
In addition to conditional checking with two functions, assert and require, there are two other ways to trigger an exception:
The revert function can be used to mark errors and roll back the current call
Use the throw keyword to throw an exception (from version 0.4.13, the throw keyword has been deprecated and will be eliminated in the future. )
When an exception occurs in a child call, the exception automatically bubbles up. However, there are some exceptions: send, and the underlying functions call call, delegatecall,callcode, which return false when an exception occurs.
Note: calling the underlying function call,delegatecall,callcode on a non-existent address will also return success, so we should always give priority to checking the existence of the function when we make the call.
Here is an example to illustrate how to use require to check input conditions and assert for internal error checking:
Pragma solidity ^ 0.4.0X contract Sharer {function sendHalf (address addr) public payable returns (uint balance) {require (msg.value% 2 = = 0); / / only even numbers uint balanceBeforeTransfer = this.balance; addr.transfer (msg.value / 2) are allowed; / / if it fails, an exception is thrown and the following code does not execute assert (this.balance = = balanceBeforeTransfer-contract / 2); return this.balance }}
Let's actually run it and see how the exception occurs:
First open Remix, paste in the code, and click create contract. As shown below:
Run test 1: add 1wei (odd) to call sendHalf, and an exception occurs, as shown below:
Run Test 2: attach 2wei to call sendHalf, and it works fine.
Run Test 3: the additional 2wei and sendHalf parameters are the current contract itself. An exception occurs during the transfer because the contract cannot receive the transfer. The error prompt is similar to the figure above.
Assert type exception
An exception of type assert is automatically generated in the following scenarios:
If you cross the line, or a negative ordinal value accesses the array, such as I > = x.length or I < 0, access x [I]
If the sequence number is out of bounds, or a negative sequence value accesses a fixed-length bytesN.
The divisor is 0, such as 5max 0 or 23% 0.
Move a negative value for a binary. Such as: 5
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.