Solidity 0.6.x Features: try/catch statement

Published:

The Introduced in 0.6.0 Try/catch syntax The The greatest improvement in error handling capabilities is perhaps SoliditySince Strings are used for reasons Reverse Y Require They were published in v0.4.22. Both They Are all there Try Y Capture Keywords These rights have been reserved Since v0.5.9 Now They can be used to manage failures External Function You can make calls without having to revert the entire transaction. HoweverState changes in the called function can still be reversed, but not in the calling function.

We Are Removing from the Purists “all or nothing” The The transaction lifecycle approach isn’t practical enough to be effective.

External Handling Calls that go wrong

The The The try/catch clause lets you respond to failures. External Calls Contract Creation You You won’t have the ability to make calls, so you won’t use it Internal function calls. Note To Wrap a public function in the same contract with try/catch. It is possible to make it extern by calling the function East..

The The The following example illustrates how try/catch functions in a factory setting where contract creation cannot be achieved. Monitoring CharitySplitter The Contract requires that property is located at a particular address _owner In Your builder.

Pragma Solidity 0.6.1;

contract CharitySplitter {
 Address The public owner;
 Builder (Address _owner) Public {
 Require(_owner != Address(0), "no-owner-provided");
 Owner = _owner;
    }
}

There is a factory contract — CharitySplitterFactory This This is used for managing and creating instances. CharitySplitter. At We You can wrap the product at our factory new CharitySplitter (charityOwner) In In case the constructor is unable to complete the task due to an unexpected event, a try/catch should be used. “void”. CharityOwner being passed

pragma Solidity = 0.6.1;
Import "./CharitySplitter.sol";
contract CharitySplitterFactory {
 Map (Address => CharitySplitter) Splitters Public charity;
 uint public errorCount;
 Event ErrorHandled(String There are reasons);
 Event ErrorNotHandled(Reasons for bytes);
    Funktion createCharitySplitter(CharityOwner) Public {
 New CharitySplitter available(CharityOwner)
 Returns (CharitySplitter newCharitySplitter)
        {
 CharitySplitters[msg.sender] = newCharitySplitter;
        } Catch {
            errorCount++;
        }
    }
}

Note Only You can catch exceptions in the outer call with try/catch Errors The If the input parameter is not the, expressions will not be captured new CharitySplitter This is an internal call, and you will not be caught for any mistakes. The The An example modified illustrates this behavior. createcharitydivide function. Here The CharitySplitter constructor input parameter is dynamically retrieved from another function — GetCharityOwner. If This Example 1 shows how the reverse function works. “revert-required-for-test”This The try/catch sentence will not apply to you.

Funktion createCharitySplitter(Address _charityOwner) Public {
 New CharitySplitter available(GetCharityOwner(_charityOwner, False))
 Returns (CharitySplitter newCharitySplitter)
    {
 CharitySplitters[msg.sender] = newCharitySplitter;
    } Catch (Bytes Memory reason) {
        ...
    }
}
Funktion getCharityOwner(Address _charityOwner. bool)
 Internal Returns (Address) {
 Require(_toPass, "revert-required-for-testing");
    Return _charityOwner;
}

Retrieving The error message

We You The try/catch logic can be extended to the CreatecharityDivide function to retrieve the error message sent by an error Reverse Or Both Require It Broadcasts can be made at events There Two There are several ways you can do this:

1. Using Catch error (string memory reason).

Funktion createCharitySplitter(Address _charityOwner) Public {
 New CharitySplitter available(_charityOwner) Returns (CharitySplitter newCharitySplitter)
    {
 CharitySplitters[msg.sender] = newCharitySplitter;
    }
 Catch Error(String Memory reason)
    {
        errorCount++;
        CharitySplitter NeueCharitySplitter = New
            CharitySplitter(msg.sender);
 CharitySplitters[msg.sender] = newCharitySplitter;
        // Emitting The error In Event
        emit ErrorHandled(Reason);
    }
 Catch
    {
        errorCount++;
    }
}

Emitting The This event must take place on a building that has failed.

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

2. Using catch (byte memory speed)

Funktion createCharitySplitter(CharityOwner) Public {
 New CharitySplitter available(CharityOwner)
 Returns (CharitySplitter newCharitySplitter)
    {
 CharitySplitters[msg.sender] = newCharitySplitter;
    }
 Catch (Bytes Memory reason) {
        errorCount++;
        emit ErrorNotHandled(Reason);
    }
}

Emitting The This event must take place on a building that has failed.

CharitySplitterFactory.ErrorNotHandled(
 Reason: hex'08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000116e6f2d6f776e65722d70726f7669646564000000000000000000000000000000' (Type: bytes)

The Both Similar results can be obtained using the other methods of retrieving error strings. The The The second method does NOT ABI encode an error string. The The This second method has the advantage that it can be used in the event of an error string which requires ABI decoding, or if there’s no explanation.

Future Plans

There We We are going to provide support for all types of errors. This This will allow us to report errors in a similar way to events. This We will be able to catch various types of errors like:

Get CustomErrorA(uint data1) {}
Get CustomErrorB(Uint[] Memory data2) {}
Catch {}

Related articles

Recent articles