#100DaysOfSolidity Solidity’s Try Catch: Simplifying Error Handling in Smart Contracts

#100DaysOfSolidity Series 036 “Try Catch”

Solidity Academy
3 min readJul 12, 2023

🔍 Solidity, the programming language for Ethereum smart contracts, offers a powerful error handling mechanism known as try-catch. 🛠️ This feature allows developers to gracefully handle exceptions and errors that may occur during the execution of their code. In this article, we will dive deep into the try-catch construct in Solidity, exploring its various applications and providing you with comprehensive examples.

#100DaysOfSolidity Solidity’s Try Catch: Simplifying Error Handling in Smart Contracts

Let’s explore how Solidity’s try-catch simplifies error handling in smart contracts. 📚

Understanding the Try-Catch Construct in Solidity 💡

The try-catch construct in Solidity allows developers to catch errors that occur during external function calls or contract creations. It acts as a safety net, providing a fallback mechanism to mitigate any potential issues. The try-catch block consists of a “try” statement, followed by one or more “catch” statements. Now, let’s explore the different use cases of try-catch in Solidity. 🤔

Handling Errors in External Function Calls ⚙️

One of the primary use cases of try-catch in Solidity is handling errors that may arise during external function calls. 📞 In the provided code sample, we have two contracts, `Foo` and `Bar`. The `Bar` contract utilizes an instance of the `Foo` contract to demonstrate the try-catch mechanism with external function calls. Let’s analyze the code snippet and its output. 🔄

function tryCatchExternalCall(uint _i) public {
try foo.myFunc(_i) returns (string memory result) {
emit Log(result);
} catch {
emit Log("external call failed");
}
}

In the `tryCatchExternalCall` function, we attempt to call the `myFunc` function of the `foo` contract instance. If the call succeeds, the returned result is emitted via the `Log` event. However, if an error occurs, such as a failed `require` statement, the catch block is executed, and the “external call failed” message is emitted. This way, you can handle errors and provide appropriate feedback to users or other contracts interacting with your smart contract. 🚀

Handling Errors in Contract Creations 🏗️

Another significant use case of try-catch in Solidity is handling errors during contract creations. 🏭 In the provided code snippet, the `Bar` contract demonstrates the try-catch mechanism when creating a new instance of the `Foo` contract. Let’s analyze the code snippet and its output. 🔄

function tryCatchNewContract(address _owner) public {
try new Foo(_owner) returns (Foo foo) {
// you can use variable foo here
emit Log("Foo created");
} catch Error(string memory reason) {
// catch failing revert() and require()
emit Log(reason);
} catch (bytes memory reason) {
// catch failing assert()
emit LogBytes(reason);
}
}

In the `tryCatchNewContract` function, we attempt to create a new instance of the `Foo` contract with the provided `_owner` address. If the contract creation is successful, the `Foo created` message is emitted via the `Log` event. However, if an error occurs during the contract creation, such as an invalid address or a failed `assert` statement, the appropriate catch block is executed. It emits an error message via the `Log` event or provides additional information through the `LogBytes` event. This way, you can handle errors during contract creations and take appropriate actions accordingly. 🛠️

Conclusion 🎉

Solidity‘s try-catch construct provides developers with a powerful error handling mechanism for dealing with exceptions and errors during external function calls and contract creations. By utilizing try-catch, you can gracefully handle these errors, improving the robustness and reliability of your smart contracts. In this article, we explored the different use cases of try-catch in Solidity, providing comprehensive code samples and explanations. We hope this article has enhanced your understanding of try-catch in Solidity and empowered you to write more secure and error-resilient smart contracts. Happy coding! 💪🖥️

🔗 Additional Resources:

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/