MySQL stored procedures support error handling through DECLARE ... HANDLER statements. Handlers let you catch SQL errors and warnings, take corrective action, and prevent procedures from aborting unexpectedly.
Handler Types
- CONTINUE handler: Catches the error and continues execution after the statement that caused it
- EXIT handler: Catches the error and exits the innermost BEGIN...END block
Condition Values
- SQLEXCEPTION — any SQL error
- SQLWARNING — any SQL warning (01xxx)
- NOT FOUND — no rows found (02xxx)
- Specific SQLSTATE codes, e.g., '23000' for duplicate key
Using SIGNAL
The SIGNAL statement lets you raise a custom error from within a procedure. This is useful for enforcing business rules and returning meaningful error messages to the caller.