Writing contracts fundamental purpose of Solidity. However, writing a contract demands sound error and exception handling. Errors and exceptions are the norms in programming and Solidity provides ample infrastructure for managing both. Writing robust contracts with proper error and exception management is best practices. Events are another important construct in Solidity. We’ve known a caller that invokes functions in contracts; however, not discussed any mechanism through which a contract notifies its caller about changes in its state and otherwise. where events . Events are of event-driven programs where, changes within a program, it proactively notifies its caller about the changes. The caller is use this information or ignore it. Finally, both exceptions and events, to extent, use the logging feature provided by EVM.
Errors are often inadvertently introduced while writing contracts, so writing robust contracts good practice be followed. Errors are a fact of life programming world and writing error-free contracts desired skill. Errors can occur at design time or runtime. Solidity is compiled into bytecode and there are design-level checks for any syntax errors at design time while compiling. Runtime errors, however, are more difficult to catch occur while executing contracts. important the contract for possible runtime errors, but more important defensive and robust contracts that at both design time and runtime errors. runtime errors are out-of-gas errors, divide by zero errors, data type overflow errors, array-out-of-index errors, and so on. Until version 4.10 of Solidity, there was throw statement available for error handling. Developers had multiple if…else statements the values and throw in the case of . The throw statement consumes all the provided gas and reverts to state. not situation for architects and developers as unused gas should be returned back to the caller. From version 4.10 of Solidity newer error handling constructs were introduced throw
was made obsolete. These were the assert, require, and revert statements. Iworth noting that no try..catch statements or constructs to catch errors and exceptions.
The require statement
The word requires denotes constraints. Declaring require statements means declaring prerequisites for running the function; in other words, it means declaring constraints be satisfied before executing lines of code. statement takes single argument: that evaluates to or false boolean value. If the evaluation of the statement , an exception is raised and execution is halted. The unused gas is returned to the caller state is reversed to . statement the revert opcode, which is reverting the state and returning unused gas.
The following code illustrates of statement:
Let’s take at functions depicted preceding screenshot:
1. ValidInt8: This function uses of the required statements. In constructs, checks for values greater than or zero. If this statement is true, execution passes to the statement. If this statement , an exception is thrown and execution stops. requires statement checks whether than or 255. If the argument than 255, the statement evaluates to false and throws an exception.
2. ShouldbeEven: This function is of nature. This function, require checks whether the incoming argument is even or odd. If the argument is even, execution passes to the statement; otherwise, an exception is thrown. statement should be used for validating all arguments and values that are incoming to the function. that if another function from another contract or function same contract , the incoming value should checked using function. function should be check state of variables before used. If required throws an exception, it should mean that the values passed to the function expected by the function the caller should modify before sending it to a contract.
The assert statement
The assert statement similar syntax to statement. If it accepts , then evaluate to either or false value. that, the execution will either to the statement or throw an exception. The unused gas returned to the caller and instead, gas supply is consumed by assert. The state is reversed to . The assert function an invalid opcode, which is reverting the state and consuming all gas. The function shown previously has been extended an addition to variable. However, remember that adding two variables in an overflow exception. verified using the assert statement; if it returns true, is returned, otherwise, the exception is thrown.
The following screenshot illustrates of the assert function:
While require should be used for values coming from , assert should be used for validating state and condition of the function and contract before execution. Casserting as working with runtime exceptions that wecannot predict. The assert statement should be used think that a current state has become inconsistent execution continue.
The revert statement
The revert statement function. However, it evaluate any statement and any state or statements. Hitting a revert statement means an exception is thrown, the return of unused gas, and reverts to its original state. following example, an exception is thrown when the incoming value is checked using the if condition; if the if condition evaluation false, it executes the revert function. This an exception and execution stops, as shown following screenshot: