Skip to content

Instantly share code, notes, and snippets.

@daltyboy11
Created April 11, 2022 16:49
Show Gist options
  • Save daltyboy11/1c2a416b0e2543a89fe1882be1d14747 to your computer and use it in GitHub Desktop.
Save daltyboy11/1c2a416b0e2543a89fe1882be1d14747 to your computer and use it in GitHub Desktop.
Solidity try/catch demonstration
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
/*
You can only apply a try/catch block to a function called externally. An external
function call is a message call, whereas an internal function call is just a jump.
This snippet shows the same function, f1(), called internally and externally. The
internal call code does not compile.
*/
contract Foo {
function f1() public pure {
revert("Why are you calling me?!");
}
// // This function doesn't compile
// function callF1Internal() external returns (bool) {
// try f1() {
// return true;
// } catch {
// return false;
// }
// }
// This function compiles
function callF1External() external returns (bool) {
try this.f1() {
return true;
} catch {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment