Last active
April 22, 2023 13:51
-
-
Save ashwinYardi/bcc4f9889b454f21facbd22d03affe55 to your computer and use it in GitHub Desktop.
Solidity contract to demo try / catch statements and error retrieval.
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.0; | |
contract HelloWorld { | |
function showMessage() public pure returns (string memory) { | |
return "Hello, World!"; | |
} | |
} | |
contract HelloWorldFactory { | |
mapping (address => HelloWorld) public contracts; | |
string public errorMessage; | |
bytes public errorData; | |
function createHelloWorld() public { | |
try new HelloWorld() | |
returns (HelloWorld newContract) | |
{ | |
contracts[msg.sender] = newContract; | |
} catch Error(string memory reason) { | |
errorMessage = reason; | |
} catch (bytes memory data) { | |
errorData = data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment