Created
April 11, 2022 16:49
-
-
Save daltyboy11/1c2a416b0e2543a89fe1882be1d14747 to your computer and use it in GitHub Desktop.
Solidity try/catch demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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