Skip to content

Instantly share code, notes, and snippets.

@ashwinYardi
Last active April 22, 2023 13:51
Show Gist options
  • Save ashwinYardi/bcc4f9889b454f21facbd22d03affe55 to your computer and use it in GitHub Desktop.
Save ashwinYardi/bcc4f9889b454f21facbd22d03affe55 to your computer and use it in GitHub Desktop.
Solidity contract to demo try / catch statements and error retrieval.
// 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