I have been trying to run this code:

function seeError(address _token) public view returns (uint256) {
        try IERC20(_token).decimals() {
            return IERC20(_token).decimals();
        } catch {
            revert("Error occured");

Now, since I can input any address for the token, I want to revert in case there are no decimals returned, or basically, the ERC20 address is wrong. So I tried using try/catch for this since I am unable to get the data directly. Is there any way to get the result I want? Try/catch is working for external calls only, can it work for view functions too?

1 Answer 1


Why not do something like this

function seeError(address _token) public view returns (uint256) {
       (bool succes,) = _token.call(abi.encodePacked("decimals()"));
       require (success,"Error occured");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.