Last active
April 21, 2023 14:04
-
-
Save ashwinYardi/5a5dfab6079d5267d086fc84c1dda338 to your computer and use it in GitHub Desktop.
Contract to demo how to use try/catch in Solidity.
This file contains 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; | |
uint public errorCount; | |
function createHelloWorld() public { | |
try new HelloWorld() | |
returns (HelloWorld newContract) | |
{ | |
contracts[msg.sender] = newContract; | |
} catch { | |
errorCount++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment