Skip to content

Instantly share code, notes, and snippets.

@ashwinYardi
Last active April 21, 2023 14:04
Show Gist options
  • Save ashwinYardi/5a5dfab6079d5267d086fc84c1dda338 to your computer and use it in GitHub Desktop.
Save ashwinYardi/5a5dfab6079d5267d086fc84c1dda338 to your computer and use it in GitHub Desktop.
Contract to demo how to use try/catch in Solidity.
// 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