Exception handling
Errors are a fact of life in the programming world. They are often inadvertently introduced while writing contracts and therefore writing error-free contracts is a desired skill. These errors can occur either at design time or runtime. Solidity is compiled into bytecode as part of compilation and the compiler checks for any syntax errors during this process. Runtime errors, however, are more difficult to catch and generally occur while executing contracts. It is important to test the contract for possible runtime errors, but it is more important to write defensive and robust contracts that take care of both design-time and runtime errors. Some examples of runtime errors are out-of-gas errors, divide-by-zero errors, data-type-overflow errors, and array-out-of-index errors.
Until version 4.10 of Solidity, there was a single throw
statement available for error handling. Developers had to write multiple if...else
statements to check the values and throw in the case...