Skip to content

Instantly share code, notes, and snippets.

@merlinboii
Last active February 3, 2024 11:48
Show Gist options
  • Select an option

  • Save merlinboii/a1f1802ec95ce3bce12079fb7df33ccc to your computer and use it in GitHub Desktop.

Select an option

Save merlinboii/a1f1802ec95ce3bce12079fb7df33ccc to your computer and use it in GitHub Desktop.
Try/Catch of solidity by example
contract ExternalContract {
function myFunc() public pure returns (bool) {
return true;
}
function myFuncRevert() public pure {
revert("revert-external-call");
}
}
contract TryCatchExample1 {
uint256 currentBalance = 0;
ExternalContract public externalContract;
event Log(bool success, string message);
event LogBytes(bytes data);
constructor() {
externalContract = new ExternalContract();
}
function tryCatchRevertExternalCall() public {
try externalContract.myFunc() returns (bool result) {
externalContract.myFuncRevert();
emit Log(result, "");
} catch Error(string memory reason) {
// catch failing revert() and require()
emit Log(false, reason);
} catch (bytes memory reason) {
// catch failing assert()/panic()
emit LogBytes(reason);
}
}
function tryCatchRevertLocal() public {
try externalContract.myFunc() returns (bool result) {
currentBalance = currentBalance - 10;
emit Log(result, "");
} catch Error(string memory reason) {
// catch failing revert() and require()
emit Log(false, reason);
} catch (bytes memory reason) {
// catch failing assert()/panic()
emit LogBytes(reason);
}
}
}
contract TryCatchExample2 {
ExternalContract public externalContract;
event Log(bool success, string message);
event LogBytes(bytes data);
constructor() {
externalContract = new ExternalContract();
}
function tryCatchRevertNonExistAddress() public {
try ExternalContract(address(0)).myFunc() returns (bool result) {
externalContract.myFuncRevert();
emit Log(result, "");
} catch Error(string memory reason) {
// catch failing revert() and require()
emit Log(false, reason);
} catch (bytes memory reason) {
// catch failing assert()/panic()
emit LogBytes(reason);
}
}
}
contract ExternalContract {
function myFunc() public pure returns (bool) {
return true;
}
function myFuncRevert() public pure {
revert("revert-external-call");
}
function myFuncReturn() public pure returns (bytes32) {
return keccak256("myFuncReturnBytes");
}
}
interface IExternalContract {
function myFuncReturn() external view returns(string memory);
}
contract TryCatchExample3 {
ExternalContract public externalContract;
event Log(bool success, string message);
event LogBytes(bytes data);
constructor() {
externalContract = new ExternalContract();
}
function tryCatchReturnEncoding() public {
try IExternalContract(address(externalContract)).myFuncReturn() returns (string memory result) {
emit Log(true, result);
} catch Error(string memory reason) {
// catch failing revert() and require()
emit Log(false, reason);
} catch (bytes memory reason) {
// catch failing assert()/panic()
emit LogBytes(reason);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment