Crypto Flexs
  • DIRECTORY
  • CRYPTO
    • ETHEREUM
    • BITCOIN
    • ALTCOIN
  • BLOCKCHAIN
  • EXCHANGE
  • TRADING
  • SUBMIT
Crypto Flexs
  • DIRECTORY
  • CRYPTO
    • ETHEREUM
    • BITCOIN
    • ALTCOIN
  • BLOCKCHAIN
  • EXCHANGE
  • TRADING
  • SUBMIT
Crypto Flexs
Home»ETHEREUM NEWS»Solidity 0.6.x feature: try/catch statement
ETHEREUM NEWS

Solidity 0.6.x feature: try/catch statement

By Crypto FlexsFebruary 13, 20244 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email
Solidity 0.6.x feature: try/catch statement
Share
Facebook Twitter LinkedIn Pinterest Email

that much try/catch syntax introduced in 0.6.0 This is arguably the biggest leap forward in Solidity’s error handling capabilities. going back and need Released in v0.4.22. both try hard and Catch This is a reserved keyword From v0.5.9 You can now use this to handle errors. Out Calling a function without rolling back the entire transaction (state changes in the called function will still be rolled back, but state changes in the calling function will not)

We are moving one step further from the purist “all or nothing” approach to the transaction lifecycle. This falls short of the actual behavior we often want.

External call failure handling

Try/catch statements allow you to react to failure. Out With a phone Create contract I can’t use it because I’m making a phone call. interior Function call. If you want to wrap a public function call within the same contract using try/catch, you can make it external by calling the function with: this..

The example below shows how try/catch is used in the factory pattern where contract creation may fail. as follows charity dispenser The contract requires the required address attribute. _owner In the 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 — charity division factory Used to create and manage instances. charity dispenser. From factory we can pack New CharitySplitter (charityOwner) Used in try/catch as a failsafe for cases where an empty value may cause the constructor to fail. charity owner It’s 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++;
        
    

With try/catch, only exceptions that occur within the external call itself are caught. For example, errors inside expressions are not caught. New CharitySplitter Since it is itself part of the internal call, any errors that occur will not be caught. The sample demonstrating this behavior has been modified. createCharitySplitter function. here charity dispenser Constructor input parameters are retrieved dynamically from other functions. getCharityOwner. If that function reverts, in this example “Revert needed for testing”This is not 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;

Search for error messages

You can extend the try/catch logic further: createCharitySplitter A function to retrieve the error message if a failure resulted in an error message. going back or need Exported as event. There are two ways to achieve this:

1. Use Catching errors (string memory reasons)

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++;
    

An error occurs when the following event occurs in a failed constructor:

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

2. Use catch(bytememoryreason)

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

An error occurs when the following event occurs in a failed constructor:

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

The above two methods of searching for error strings produce similar results. The difference is that the second method does not ABI decode the error string. The advantage of the second method is that it runs even if ABI decoding fails on an error string or no reason is provided.

Future Plans

There are plans to release support for this error type. This means that you will be able to catch many different types of errors by declaring them in a similar way to events. For example:

catch CustomErrorA(uint data1)  … 
catch CustomErrorB(uint() memory data2)  … 
catch 
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

Related Posts

Class action lawsuit claims Microsoft choked AI supply to drive up ChatGPT costs

October 14, 2025

‘OG’ Whale Attracts Over $400 Million in Bearish BTC Bet

October 10, 2025

BitDigital becomes the first public Etherrium for distributing unsecured leverage -details -Details

October 6, 2025
Add A Comment

Comments are closed.

Recent Posts

US government holds $36 billion in Bitcoin after largest confiscation in history

October 15, 2025

Decoding City Protocol’s IP Capital Market

October 14, 2025

Tria Raises $12M To Be The Leading Self-custodial Neobank And Payments Infrastructure For Humans And AI.

October 14, 2025

How to Use Google Gemini to Analyze Crypto Coins Before Investing

October 14, 2025

Class action lawsuit claims Microsoft choked AI supply to drive up ChatGPT costs

October 14, 2025

CME Group Launches CFTC Regulated Solana and XRP Options

October 13, 2025

Eightco Holdings Inc. ($ORBS) Makes Strategic Investment Into Mythical Games To Accelerate Human Verification And Digital Identity In Gaming

October 13, 2025

Jiuzi Holdings, Inc. (JZXN) Secures 100 Bitcoin Via Private Placement, Signaling New Phase In Crypto Treasury Deployment

October 13, 2025

Collaboration Across Bybit, DigiFT And UBS UMINT Expands Collateral Solution For Institutions

October 13, 2025

BitMine Immersion (BMNR) Announces ETH Holdings Exceeding 3.03 Million Tokens And Total Crypto And Cash Holdings Of $12.9 Billion

October 13, 2025

Phemex Announces Halloween Futures Trading Festival With 200,000 USDT Prize Pool

October 13, 2025

Crypto Flexs is a Professional Cryptocurrency News Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of Cryptocurrency. We hope you enjoy our Cryptocurrency News as much as we enjoy offering them to you.

Contact Us : Partner(@)Cryptoflexs.com

Top Insights

US government holds $36 billion in Bitcoin after largest confiscation in history

October 15, 2025

Decoding City Protocol’s IP Capital Market

October 14, 2025

Tria Raises $12M To Be The Leading Self-custodial Neobank And Payments Infrastructure For Humans And AI.

October 14, 2025
Most Popular

Floki and TokenFi are targeting the MENA region to expand the cryptocurrency market.

March 28, 2024

BNB price signals bullish exhaustion, is bearish attractive in the near term?

January 18, 2024

NVIDIA Grace CPU: ETL efficiency of polars and apache spark

March 12, 2025
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
© 2025 Crypto Flexs

Type above and press Enter to search. Press Esc to cancel.