Writing contracts is that the fundamental purpose of Solidity. However, writing a contract demands sound error and exception handling. Errors and exceptions are the norms in programming and Solidity provide ample infrastructure for managing both. Writing robust contracts with proper error and exception management is one among the highest best practices. Events are another important construct in Solidity. We’ve known a caller that invokes functions in contracts; however, we’ve not discussed any mechanism through which a contract notifies its caller et al. about changes in its state and otherwise. this is often where events are available . Events are a neighborhood of event-driven programs where, supported changes within a program, it proactively notifies its caller about the changes. The caller is liberal to use this information or ignore it. Finally, both exceptions and events, to an outsized extent, use the logging feature provided by EVM.
Errors are often inadvertently introduced while writing contracts, so writing robust contracts may be a good practice and will be followed. Errors are a fact of life within the programming world and writing error-free contracts may be a 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 and usually occur while executing contracts. it’s important to check the contract for possible runtime errors, but it’s more important to write down defensive and robust contracts that lookout of both design time and runtime errors. samples of 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 one throw statement available for error handling. Developers had to write down multiple if…else statements to see the values and throw in the case of a mistake . The throw statement consumes all the provided gas and reverts to the first state. this is often not a perfect 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 and therefore the throw
was made obsolete. These were the assert, require, and revert statements. it’s worth noting that there’s 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 that ought to be satisfied before executing the subsequent lines of code. the specified statement takes during a single argument: a press release that evaluates to a real or false boolean value. If the evaluation of the statement is fake , an exception is raised and execution is halted. The unused gas is returned to the caller and therefore the state is reversed to the first . the specified statement leads to the revert opcode, which is liable for reverting the state and returning unused gas.
The following code illustrates the utilization of the specified statement:
The require statement
Let’s take a glance at the subsequent functions depicted within the preceding screenshot:
- ValidInt8: This function uses a few of required statements. In constructs, a press release checks for values greater than or adequate to zero. If this statement is true, execution passes to the subsequent statement. If this statement is fake , an exception is thrown and execution stops. subsequent requires statement checks whether the worth is a smaller amount than or adequate to 255. If the argument is bigger than 255, the statement evaluates to false and throws an exception.
- ShouldbeEven: This function is of an identical nature. This function, require checks whether the incoming argument is even or odd. If the argument is even, execution passes to the subsequent statement; otherwise, an exception is thrown. the specified statement should be used for validating all arguments and values that are incoming to the function. this suggests that if another function from another contract or function within the same contract is named , the incoming value should even be checked using the specified function. the specified function should be wont to check the present state of variables before they’re used. If require throws an exception, it should mean that the values passed to the function weren’t expected by the function which the caller should modify the worth before sending it to a contract. The assert statement The assert statement features a similar syntax to the specified statement. If it accepts a press release , that ought to then evaluate to either a real or false value. supported that, the execution will either advance to subsequent statement or throw an exception. The unused gas isn’t returned to the caller and instead, the whole gas supply is consumed by assert. The state is reversed to the first . The assert function leads to an invalid opcode, which is liable for reverting the state and consuming all gas. The function shown previously has been extended to incorporate an addition to the prevailing variable. However, remember that adding two variables may result in an overflow exception. this is often verified using the assert statement; if it returns true, the worth is returned, otherwise, the exception is thrown. The following screenshot illustrates the utilization of the assert function:
The assert statement
While require should be used for values coming from the surface , assert should be used for validating the present state and condition of the function and contract before execution. Consider asserting as working with runtime exceptions that we simply cannot predict. The assert statement should be used once you think that a current state has become inconsistent which execution shouldn’t continue.
The revert statement
The revert statement is extremely almost like the specified function. However, it doesn’t evaluate any statement and doesn’t depend upon any state or statements. Hitting a revert statement means an exception is thrown, along side the return of unused gas, and reverts to its original state. within the following example, an exception is thrown when the incoming value is checked using the if condition; if the if condition evaluation leads to false, it executes the revert function. This leads to an exception and execution stop, as shown within the following screenshot:
The revert statement
For more details visit:https://www.technologiesinindustry4.com/solidity-exception-and-error-handling/