43: Try and Catch
In Solidity try and catch can be used to handle exceptions. This can prevent a transaction from failing due to an invalid input or a bug in the code.
Example 1 In the following example, we want to prevent someone from sending more than 5 ether in a single transaction.
In this example, if the _amount is greater than 5 ether, the require statement will throw an exception and the catch block will handle the exception, reverting the transaction and sending a message to the sender.
Example 2 In this example, we want to ensure that the sender and receiver are not the same address.
If the sender and receiver are the same address, the require statement will throw an exception and the catch block will handle the exception, reverting the transaction and sending a message to the sender.
That's it for the lesson 43! In the next lesson, Library
Last updated