Skip to content

try/catch doesn't catch some call errors. #12725

Open
@drortirosh

Description

@drortirosh

The following code is expected (by a developer using any other language) to catch failures related to the call.
But instead, it reverts.
This is due to the very confusing semantics of the "try/catch" construct in solidity: the catch block will only be called if the actual external code was reverted, but it might still revert in some compiler-generated code

pragma solidity ^0.8.12;

interface Xface {
    function func() external;
}

contract Test {
    function run() external {
        try Xface(address(0)).func()  {
            console.log('successfully called');
        } catch {
            console.log('catch everything');
        }
    }
}

Suggestion: solidity should not generate a "revert" code between the try and catch. Instead, it should jump into the catch block.

Currently, the following checks are done by solidity, and it reverts if any of these validation fails:

  • if the called method is "void", it first performs extcodesize, to validate the target indeed has some code
  • if the called method is expected to return something, it validates that returndatasize is not zero
  • if there are returned values, it decodes them and of course validates they were encoded correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking change ⚠️language design :rage4:Any changes to the language, e.g. new featuresmedium effortDefault level of effortmedium impactDefault level of impactmust haveSomething we consider an essential part of Solidity 1.0.needs designThe proposal is too vague to be implemented right away

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions