Skip to content

Instantly share code, notes, and snippets.

@deliriusz
Last active July 19, 2023 14:25
Show Gist options
  • Save deliriusz/6c05146893083b344fc859b49928b984 to your computer and use it in GitHub Desktop.
Save deliriusz/6c05146893083b344fc859b49928b984 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract TryCatchCaller {
TryCatchCallee calee;
constructor () {
calee = new TryCatchCallee();
}
function callMe() public {
console.log("inside TryCatchCaller.callMe()");
}
function test() public {
try calee.test() {
console.log("TryCatchCallee.test() ran successful");
} catch {
console.log("TryCatchCallee.test() failed");
}
}
// will not compile
function test2() public {
try callMe() {
console.log("TryCatchCaller.callMe() ran successful");
} catch {
console.log("TryCatchCaller.callMe() failed");
}
}
// totally ok
function test3() public {
try TryCatchCaller(address(this)).callMe() {
console.log("TryCatchCaller.callMe() ran successful");
} catch {
console.log("TryCatchCaller.callMe() failed");
}
}
// totally ok
function test4() public {
try this.callMe() {
console.log("this.callMe() ran successful");
} catch {
console.log("this.callMe() failed");
}
}
}
contract TryCatchCallee {
function test() public {
console.log("inside TryCatchCallee.test()");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment