DEV Community

Loading Blocks
Loading Blocks

Posted on

Solidity Errors & Exceptions

Core Mechanism

When error occurs the transaction is reverted and all state changes are undone.

three main ways

  1. require(): Validate external input and preconditions. when error occurs it will refund Gas. require(msg.sender == owner, "You are not the owner.");
  2. assert(): It should only be used for internal errors and invariants. assert(balance == 50);
  3. revert(): It is used for errors of complex logic and also refund Gas when error occurs.
contract ErrorHandling {

   error NotOwner();

   if (msg.sender != owner) {
     revert NotOwner();  //Custom Errors
   }
}
Enter fullscreen mode Exit fullscreen mode

Custom Errors

Custom errors save gas compared to string error messages.

Top comments (0)