Try And Catch In Solidity

 

blog

Introduction


As of Solidity version 0.6.0, the language has built-in try-catch functionality. The syntax is similar to that of other programming languages. However, currently, the functionality is limited to trying expressions used in external function calls or contract creation. Try and Catch can now handle failures in external function calls without rolling back the entire transaction (state changes in the called function are still rolled back, but not those in the calling function).


Here is an example of how to use the try-catch mechanism in a Solidity contract:


// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract NumberChecker {

function checkNum(int a, int b) public pure{

require(a > 0 && b > 0, "Cannot Divide by 0");

}

}

contract ErrorHandling {

event ErrorLogging(string reason);

function divide(int a, int b) public returns(int result) {

NumberChecker numCh = new NumberChecker();

try numCh.checkNum(a,b){

return a/b;

} catch Error(string memory reason) {

emit ErrorLogging(reason);

}

}

}

Sample of Solidity Contract 


This sample Solidity contract demonstrates the use of try-catch blocks for error handling. The contract has two parts: the NumberChecker contract and the ErrorHandling contract.


The NumberChecker contract has a single function called "checkNum" that takes in two integers, "a" and "b." Inside the function, a require statement is used to check whether "a" and "b" are greater than 0. If either "a" or "b" is less than or equal to 0, the required statement will fail, and an error message "Cannot Divide by 0" will be thrown.


The ErrorHandling contract has two main components: an event called "ErrorLogging" and a function called "divide." The "ErrorLogging" event logs any errors that occur during the execution of the contract.


The "divide" function takes in two integers, "a" and "b," and returns the result of dividing "a" by "b." Inside the function, a new instance of the NumberChecker contract is created, and the "checkNum" function is called with the input variables "a" and "b."


Then the try block is used to perform the division operation, and if the checkNum function throws an error, it will be caught by the catch block. The "ErrorLogging" event is emitted inside the catch block, and the reason for the error is passed as a parameter. This allows the user to track the error and take appropriate action.


You can see in the logs on the bottom right side that the transaction is mined successfully. And when you open the transaction details, you see an event with the error message:


The above-mentioned expressions are the only ones the try portion of the try-catch functionality can handle. Reversions that occur inside of try blocks won't be caught.


Another way for using catch is a catch (bytes memory lowLevelData). This catch block handles all other cases. This block is executed if the error signature does not match any other clause, there was an error during error message decoding, a failing assertion in the external call, or no error data was provided with the exception.


Try and Catch is only available for external calls. If we want to use the try/catch pattern with internal calls within a contract, we can use “this” global variable to call an internal function as if it were an external call. For ex:-


// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Demo{

mapping(uint256 => string) votes;

event Voted(uint256);

event LogError(string);

}


Using the try-catch mechanism, you can ensure your smart contract can continue functioning even in unexpected errors, making it more robust and secure.


Conclusion


Try-catch is a powerful mechanism in solidity to handle errors, it allows to catch specific errors and emits events and also can be used to handle external calls. Using it in your smart contract is essential to ensure its robustness and security.

Comments

Popular posts from this blog

Building a Cryptocurrency Exchange Platform: Key Considerations & Best Practices

Blockchain Security: Safeguarding the Decentralized Future

Build Dynamic Websites With Jamstack Web Development