Posts

Showing posts with the label Try Catch

Try And Catch In Solidity

Image
  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 memor