0
0
MySQLquery~20 mins

Error handling in procedures in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a procedure with DECLARE CONTINUE HANDLER
Consider the following MySQL procedure. What will be the output when calling CALL test_error_handling();?
MySQL
DELIMITER $$
CREATE PROCEDURE test_error_handling()
BEGIN
  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
  BEGIN
    SELECT 'Error caught';
  END;
  -- This will cause a division by zero error
  SELECT 10 / 0;
  SELECT 'No error';
END$$
DELIMITER ;
AError caught
BProcedure completes without output
CNo error
DSyntax error in procedure
Attempts:
2 left
💡 Hint
Think about what DECLARE CONTINUE HANDLER does when an error occurs inside the procedure.
🧠 Conceptual
intermediate
1:30remaining
Purpose of DECLARE EXIT HANDLER in MySQL procedures
What is the main difference between a DECLARE CONTINUE HANDLER and a DECLARE EXIT HANDLER in MySQL stored procedures?
ABoth handlers behave the same and continue execution after handling the error.
BCONTINUE HANDLER stops the procedure after handling the error; EXIT HANDLER allows it to continue.
CEXIT HANDLER is used only for warnings, CONTINUE HANDLER only for errors.
DCONTINUE HANDLER allows the procedure to continue after handling the error; EXIT HANDLER stops the procedure after handling the error.
Attempts:
2 left
💡 Hint
Think about what happens to the procedure flow after the handler runs.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this error handler declaration
Which option correctly fixes the syntax error in this MySQL procedure snippet?
MySQL
CREATE PROCEDURE sample_proc()
BEGIN
  DECLARE HANDLER FOR SQLEXCEPTION
  BEGIN
    SELECT 'Error';
  END;
END;
ARemove BEGIN and END inside handler block
BAdd EXIT keyword: DECLARE EXIT HANDLER FOR SQLEXCEPTION
CChange to DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
DNo change needed, syntax is correct
Attempts:
2 left
💡 Hint
MySQL requires a handler type keyword before HANDLER.
🔧 Debug
advanced
2:30remaining
Why does this procedure not catch the error as expected?
Given this procedure, why does the error not get caught by the handler?
MySQL
DELIMITER $$
CREATE PROCEDURE test_proc()
BEGIN
  DECLARE EXIT HANDLER FOR SQLEXCEPTION
  BEGIN
    SELECT 'Error caught';
  END;
  SET @x = 1/0;
  SELECT 'No error';
END$$
DELIMITER ;
AThe handler is EXIT type, so it exits before printing 'Error caught'
BDivision by zero in SET statement does not raise SQLEXCEPTION
CSET @x = 1/0 is evaluated outside the procedure context, so handler does not catch it
DThe error occurs before the handler is declared
Attempts:
2 left
💡 Hint
Consider where the error happens and how MySQL evaluates expressions.
optimization
expert
3:00remaining
Optimizing error handling for multiple error codes
You want to catch multiple specific SQL error codes in a MySQL procedure and handle them differently. Which approach is the most efficient and correct?
ADeclare multiple handlers, one for each error code, each with its own handler block
BUse a single EXIT handler for all errors and re-raise errors for unhandled codes
CDeclare one handler for SQLWARNING and ignore error codes
DDeclare one handler for SQLEXCEPTION and use IF statements inside to check error codes
Attempts:
2 left
💡 Hint
MySQL allows multiple handlers for different conditions.