Solidity — All About Runtime Errors
--
This article is Part II of the “All About Errors” sub-series.
After looking at Compile Time errors in Part I (errors generated by the Solidity compiler), we will now look in Part II at runtime errors (errors generated at the time you interact with a contract deployed on a live network).
As we will see, 4 main types of errors can be generated in Solidity: Error(string)
, Panic(uint256), custom error
and invalid
. We will cover the rules and semantics of each of them in this article. Finally, we will have a quick glance at some potential predictions for the new error types that might be added to the Solidity programming language.
Table of content
- Common examples of errors in Solidity
- Types of Solidity errors
- Error(string)
- Panic(uint256)
- Panic Error - Example
- Panic(uint256) error codes
- Custom Errors
- How to define custom errors?
- Why using custom errors over string errors?
- Named parameters for custom errors
- Natspec comments for custom errors
- Custom Errors are part of the ABI
- Invalid
- Future Type of Errors in Solidity
Common examples of errors in Solidity
Many scenarios exist where a runtime error could occur among the Solidity code of a contract.
Some of the standard runtime errors related to Solidity include:
- When
require()
is called with the arguments which result as false. - When creating a contract using the
new
keyword fails, and the process does not end properly. - When a codeless (
address.code.length
) contract is targeted to an external function. (be aware of theisContract()
culprit when it is running through aconstructor
) - When ethers (=
msg.value
) is sent while calling a public getter (view
) orpure
method from a contract. - when ethers (=
msg.value
) are sent to a function in a contract that is not marked aspayable
. - When a condition in an
assert()
isfalse
. - When a zero-initialised variable of
function
type is called. - When a large or negative value is converted to an
enum
. - When accessing an array in an index which is too big or…