Core Mechanism
When error occurs the transaction is reverted and all state changes are undone.
three main ways
- require(): Validate external input and preconditions. when error occurs it will refund Gas.
require(msg.sender == owner, "You are not the owner.");
- assert(): It should only be used for internal errors and invariants.
assert(balance == 50);
- 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
}
}
Custom Errors
Custom errors save gas compared to string error messages.
Top comments (0)