Business Insights
  • Home
  • Crypto
  • Finance Expert
  • Business
  • Invest News
  • Investing
  • Trading
  • Forex
  • Videos
  • Economy
  • Tech
  • Contact

Archives

  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • August 2023
  • January 2023
  • December 2021
  • July 2021
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019

Categories

  • Business
  • Crypto
  • Economy
  • Finance Expert
  • Forex
  • Invest News
  • Investing
  • Tech
  • Trading
  • Uncategorized
  • Videos
Apply Loan
Money Visa
Advertise Us
Money Visa
  • Home
  • Crypto
  • Finance Expert
  • Business
  • Invest News
  • Investing
  • Trading
  • Forex
  • Videos
  • Economy
  • Tech
  • Contact
Solidity 0.6.x features: try/catch statement
  • Crypto

Solidity 0.6.x features: try/catch statement

  • August 18, 2025
  • Roubens Andy King
Total
0
Shares
0
0
0
Total
0
Shares
Share 0
Tweet 0
Pin it 0

The try/catch syntax introduced in 0.6.0 is arguably the biggest leap in error handling capabilities in Solidity, since reason strings for revert and require were released in v0.4.22. Both try and catch have been reserved keywords since v0.5.9 and now we can use them to handle failures in external function calls without rolling back the complete transaction (state changes in the called function are still rolled back, but the ones in the calling function are not).

We are moving one step away from the purist “all-or-nothing” approach in a transaction lifecycle, which falls short of practical behaviour we often want.

Handling external call failures

The try/catch statement allows you to react on failed external calls and contract creation calls, so you cannot use it for internal function calls. Note that to wrap a public function call within the same contract with try/catch, it can be made external by calling the function with this..

The example below demonstrates how try/catch is used in a factory pattern where contract creation might fail. The following CharitySplitter contract requires a mandatory address property _owner in its constructor.

pragma solidity ^0.6.1;

contract CharitySplitter {
    address public owner;
    constructor (address _owner) public {
        require(_owner != address(0), "no-owner-provided");
        owner = _owner;
    }
}

There is a factory contract — CharitySplitterFactory which is used to create and manage instances of CharitySplitter. In the factory we can wrap the new CharitySplitter(charityOwner) in a try/catch as a failsafe for when that constructor might fail because of an empty charityOwner being passed.

pragma solidity ^0.6.1;
import "./CharitySplitter.sol";
contract CharitySplitterFactory {
    mapping (address => CharitySplitter) public charitySplitters;
    uint public errorCount;
    event ErrorHandled(string reason);
    event ErrorNotHandled(bytes reason);
    function createCharitySplitter(address charityOwner) public {
        try new CharitySplitter(charityOwner)
            returns (CharitySplitter newCharitySplitter)
        {
            charitySplitters[msg.sender] = newCharitySplitter;
        } catch {
            errorCount++;
        }
    }
}

Note that with try/catch, only exceptions happening inside the external call itself are caught. Errors inside the expression are not caught, for example if the input parameter for the new CharitySplitter is itself part of an internal call, any errors it raises will not be caught. Sample demonstrating this behaviour is the modified createCharitySplitter function. Here the CharitySplitter constructor input parameter is retrieved dynamically from another function — getCharityOwner. If that function reverts, in this example with “revert-required-for-testing”, that will not be caught in the try/catch statement.

function createCharitySplitter(address _charityOwner) public {
    try new CharitySplitter(getCharityOwner(_charityOwner, false))
        returns (CharitySplitter newCharitySplitter)
    {
        charitySplitters[msg.sender] = newCharitySplitter;
    } catch (bytes memory reason) {
        ...
    }
}
function getCharityOwner(address _charityOwner, bool _toPass)
        internal returns (address) {
    require(_toPass, "revert-required-for-testing");
    return _charityOwner;
}

Retrieving the error message

We can further extend the try/catch logic in the createCharitySplitter function to retrieve the error message if one was emitted by a failing revert or require and emit it in an event. There are two ways to achieve this:

1. Using catch Error(string memory reason)

function createCharitySplitter(address _charityOwner) public {
    try new CharitySplitter(_charityOwner) returns (CharitySplitter newCharitySplitter)
    {
        charitySplitters[msg.sender] = newCharitySplitter;
    }
    catch Error(string memory reason)
    {
        errorCount++;
        CharitySplitter newCharitySplitter = new
            CharitySplitter(msg.sender);
        charitySplitters[msg.sender] = newCharitySplitter;
        // Emitting the error in event
        emit ErrorHandled(reason);
    }
    catch
    {
        errorCount++;
    }
}

Which emits the following event on a failed constructor require error:

CharitySplitterFactory.ErrorHandled(
    reason: 'no-owner-provided' (type: string)
)

2. Using catch (bytes memory reason)

function createCharitySplitter(address charityOwner) public {
    try new CharitySplitter(charityOwner)
        returns (CharitySplitter newCharitySplitter)
    {
        charitySplitters[msg.sender] = newCharitySplitter;
    }
    catch (bytes memory reason) {
        errorCount++;
        emit ErrorNotHandled(reason);
    }
}

Which emits the following event on a failed constructor require error:

CharitySplitterFactory.ErrorNotHandled(
  reason: hex'08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000116e6f2d6f776e65722d70726f7669646564000000000000000000000000000000' (type: bytes)

The above two methods for retrieving the error string produce a similar result. The difference is that the second method does not ABI-decode the error string. The advantage of the second method is that it is also executed if ABI decoding the error string fails or if no reason was provided.

Future plans

There are plans to release support for error types meaning we will be able to declare errors in a similar way to events allowing us to catch different type of errors, for example:

catch CustomErrorA(uint data1) { … }
catch CustomErrorB(uint[] memory data2) { … }
catch {}
Total
0
Shares
Share 0
Tweet 0
Pin it 0
Roubens Andy King

Previous Article
eth2 quick update no. 8
  • Forex

eth2 quick update no. 8

  • August 18, 2025
  • Roubens Andy King
Read More
Next Article
The one feature that keeps me from recommending flip phones
  • Tech

The one feature that keeps me from recommending flip phones

  • August 18, 2025
  • Roubens Andy King
Read More
You May Also Like
Crypto Treasury Narrative Bears Striking Similarly to Dotcom-Era Thinking
Read More
  • Crypto

Crypto Treasury Narrative Bears Striking Similarly to Dotcom-Era Thinking

  • Roubens Andy King
  • September 27, 2025
Ethereum price hits k support as ETFs see record 5m outflow
Read More
  • Crypto

Ethereum price hits $4k support as ETFs see record $795m outflow

  • Roubens Andy King
  • September 27, 2025
On Mining | Ethereum Foundation Blog
Read More
  • Crypto

On Mining | Ethereum Foundation Blog

  • Roubens Andy King
  • September 27, 2025
Bitcoin Daily RSI At Most Oversold Level Since April — Time To Buy? 
Read More
  • Crypto

Bitcoin Daily RSI At Most Oversold Level Since April — Time To Buy? 

  • Roubens Andy King
  • September 27, 2025
The UK Needs Regulatory Clarity That Matches Ambition
Read More
  • Crypto

The UK Needs Regulatory Clarity That Matches Ambition

  • Roubens Andy King
  • September 27, 2025
Bitcoin Price Forms Bearish Evening Star Pattern On Weekly Chart, But Can Price Go Below 0,000?
Read More
  • Crypto

Bitcoin Price Forms Bearish Evening Star Pattern On Weekly Chart, But Can Price Go Below $100,000?

  • Roubens Andy King
  • September 27, 2025
Trump-Linked WLFI Burns .43M in Tokens After Recent Buyback
Read More
  • Crypto

Trump-Linked WLFI Burns $1.43M in Tokens After Recent Buyback

  • Roubens Andy King
  • September 27, 2025
On Stake | Ethereum Foundation Blog
Read More
  • Crypto

On Stake | Ethereum Foundation Blog

  • Roubens Andy King
  • September 27, 2025

Recent Posts

  • The New Rules of Building Wealth | Bullish
  • If I Were To Invest 5 Lacs in Quality Stocks For LONG TERM (2030) (Ft Saurabh Mukherjea/Rahul Jain)
  • ‘Out of Funds.’ The Van Der Beek GoFundMe Hit $2.5M. Commenters Point to the $4.76M Ranch Bought About a Month Before His Death
  • How the Quran Talks About Money, Trade and Business | Quran & The Global Economy by Nouman Ali Khan
  • From Waiter in Bangalore To ₹1Cr+ Portfolio | Financial Freedom Journey
Featured Posts
  • The New Rules of Building Wealth | Bullish 1
    The New Rules of Building Wealth | Bullish
    • February 15, 2026
  • If I Were To Invest 5 Lacs in Quality Stocks For LONG TERM (2030) (Ft Saurabh Mukherjea/Rahul Jain) 2
    If I Were To Invest 5 Lacs in Quality Stocks For LONG TERM (2030) (Ft Saurabh Mukherjea/Rahul Jain)
    • February 14, 2026
  • ‘Out of Funds.’ The Van Der Beek GoFundMe Hit .5M. Commenters Point to the .76M Ranch Bought About a Month Before His Death 3
    ‘Out of Funds.’ The Van Der Beek GoFundMe Hit $2.5M. Commenters Point to the $4.76M Ranch Bought About a Month Before His Death
    • February 14, 2026
  • How the Quran Talks About Money, Trade and Business | Quran & The Global Economy by Nouman Ali Khan 4
    How the Quran Talks About Money, Trade and Business | Quran & The Global Economy by Nouman Ali Khan
    • February 13, 2026
  • From Waiter in Bangalore To ₹1Cr+ Portfolio | Financial Freedom Journey 5
    From Waiter in Bangalore To ₹1Cr+ Portfolio | Financial Freedom Journey
    • February 12, 2026
Recent Posts
  • Federal Reserve Board – Federal Reserve Board announces approval of application by Cooperativa de Ahorro y Credito Elga, Ltda.
    Federal Reserve Board – Federal Reserve Board announces approval of application by Cooperativa de Ahorro y Credito Elga, Ltda.
    • February 12, 2026
  • Federal Reserve Board – Federal Reserve Board issues enforcement action with former employee of Regions Bank
    Federal Reserve Board – Federal Reserve Board issues enforcement action with former employee of Regions Bank
    • February 12, 2026
  • How to Invest like the Rich 0.01%?
    How to Invest like the Rich 0.01%?
    • February 11, 2026
Categories
  • Business (2,057)
  • Crypto (2,023)
  • Economy (214)
  • Finance Expert (1,687)
  • Forex (2,016)
  • Invest News (2,434)
  • Investing (2,040)
  • Tech (2,056)
  • Trading (2,024)
  • Uncategorized (2)
  • Videos (973)

Subscribe

Subscribe now to our newsletter

Money Visa
  • Privacy Policy
  • DMCA
  • Terms of Use
Money & Invest Advices

Input your search keywords and press Enter.