Skip to content

2.12. Errors#

Syntax#


(* Introduced in 0.8.4 *)
ErrorDefinition = (* error_keyword: *) ERROR_KEYWORD
(* name: *) IDENTIFIER
(* members: *) ErrorParametersDeclaration
(* semicolon: *) SEMICOLON;

(* Introduced in 0.8.4 *)
ErrorParametersDeclaration = (* open_paren: *) OPEN_PAREN
(* parameters: *) ErrorParameters
(* close_paren: *) CLOSE_PAREN;

(* Introduced in 0.8.4 *)
ErrorParameters = ((* item: *) ErrorParameter ((* separator: *) COMMA (* item: *) ErrorParameter)*)?;

(* Introduced in 0.8.4 *)
ErrorParameter = (* type_name: *) TypeName
(* name: *) IDENTIFIER?;

Error Definitions#

Errors allow you to define descriptive names and data for failure situations. Errors can be used in revert statements. In comparison to string descriptions, errors are much cheaper and allow you to encode additional data. You can use NatSpec to describe the error to the user. They can also be defined inside or outside contracts:

contract Token {
    error NotEnoughFunds(uint requested, uint available);

    function transfer(address to, uint amount) public {
        uint balance = balances[msg.sender];
        if (balance < amount)
            revert NotEnoughFunds(amount, balance);

        // Continue with the transfer...
    }
}

Note

This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.