Error Handling & Handlers in Stored Programs
Error Handling & Handlers in Stored Programs
In production applications, unhandled database errors (such as duplicate key violations, foreign key constraints, or connection drops) can cause procedures to crash mid-execution, leaving transactions dangling.
MySQL provides robust error handling through DECLARE ... HANDLER statements and the RESIGNAL command.
Declaring Handlers Syntax
Handler Action Types:
CONTINUE: Executes the handler statement and continues procedure execution at the next statement.EXIT: Executes the handler statement and immediately terminates execution of the enclosingBEGIN ... ENDblock.
Common Condition Values:
SQLEXCEPTION: Catches any general SQL error (codes not starting with 00, 01, or 02).SQLWARNING: Catches warnings.NOT FOUND: Triggers when a cursor reaches the end of data or aSELECT INTOreturns zero rows.- Specific MySQL Error Code: e.g.,
1062(Duplicate Key Entry).
Practical Example: Transactional Safe Insert with Rollback Handler
Consider a procedure that inserts a user and customer profile atomically. If duplicate email error 1062 occurs, it automatically rolls back and outputs an error message:
Throwing Custom Exceptions with SIGNAL
You can raise custom business errors using SIGNAL SQLSTATE:
Multiple Choice Questions
1. What is the difference between a CONTINUE handler and an EXIT handler in MySQL?
A. CONTINUE restarts the server; EXIT powers down MySQL B. CONTINUE executes the handler and proceeds to the next statement; EXIT executes the handler and aborts the current block C. CONTINUE only catches warnings; EXIT only catches fatal errors D. CONTINUE is deprecated Answer: B Explanation: CONTINUE resumes execution at the following line after executing the handler action; EXIT immediately terminates the enclosing BEGIN...END block.
2. Which condition category captures all general database SQL runtime errors?
A. SQLWARNING B. SQLEXCEPTION C. NOT FOUND D. SYSTEM_ERROR Answer: B Explanation: SQLEXCEPTION is the universal condition catch-all for any SQL error code.
3. What is the purpose of the SIGNAL statement?
A. Sends an email notification to the DBA B. Explicitly raises an error condition or exception with a custom SQLSTATE and message C. Flushes DNS tables D. Reconnects to the master database Answer: B Explanation: SIGNAL SQLSTATE 'code' SET MESSAGE_TEXT = '...' allows developers to throw custom application exceptions.
4. Which standard SQLSTATE code is reserved for user-defined generic application exceptions?
A. 00000 B. 45000 C. 99999 D. 23000 Answer: B Explanation: SQLSTATE '45000' is the ANSI SQL standard state reserved for unhandled user-defined exceptions.
5. What error condition is triggered when a cursor advances past the final row or a SELECT INTO finds zero matches?
A. SQLEXCEPTION B. NOT FOUND C. SQLWARNING D. ZERO_MATCH Answer: B Explanation: NOT FOUND captures occurrences where a query returns zero rows or a cursor fetch operation reaches EOF.
Creating User-Defined Functions (CREATE FUNCTION)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Control Flow: Variables, Branches & Loops | Creating User-Defined Functions (CREATE FUNCTION) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.