0
0
PostgreSQLquery~20 mins

Exception handling (BEGIN-EXCEPTION-END) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this exception block?
Consider the following PL/pgSQL block. What will be the output after execution?
PostgreSQL
DO $$
DECLARE
  result TEXT := '';
BEGIN
  BEGIN
    RAISE EXCEPTION 'Error occurred';
  EXCEPTION
    WHEN OTHERS THEN
      result := 'Exception caught';
  END;
  RAISE NOTICE '%', result;
END $$;
AException caught
BError occurred
CNo output
DSyntax error
Attempts:
2 left
💡 Hint
Look at how the inner BEGIN-EXCEPTION-END block handles the error.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this exception block
Which option correctly identifies the syntax error in this PL/pgSQL code snippet?
PostgreSQL
DO $$
BEGIN
  BEGIN
    RAISE EXCEPTION 'Oops';
  EXCEPTION
    WHEN OTHERS THEN
      RAISE NOTICE 'Caught';
  END;
END $$;
AMissing THEN keyword after WHEN OTHERS
BMissing END for inner BEGIN block
CRAISE NOTICE cannot be used inside EXCEPTION
DMissing semicolon after RAISE EXCEPTION
Attempts:
2 left
💡 Hint
Check the syntax of the EXCEPTION WHEN clause.
optimization
advanced
2:00remaining
Optimize exception handling to avoid unnecessary overhead
Which option shows the best practice to avoid performance overhead when no exception is expected?
PostgreSQL
DO $$
BEGIN
  -- Some code that rarely raises exceptions
  PERFORM some_function();
EXCEPTION
  WHEN OTHERS THEN
    RAISE NOTICE 'Exception caught';
END $$;
AUse nested BEGIN blocks with EXCEPTION inside the main block
BRemove the EXCEPTION block entirely if exceptions are rare and can be handled outside
CReplace WHEN OTHERS with WHEN SQLSTATE '23505' THEN
DKeep the EXCEPTION block as is to always catch errors
Attempts:
2 left
💡 Hint
Exception handling adds overhead even if no error occurs.
🔧 Debug
advanced
2:00remaining
Does this exception block catch the error?
Given this PL/pgSQL block, which statement about the exception handling is correct?
PostgreSQL
DO $$
BEGIN
  PERFORM 1/0;
EXCEPTION
  WHEN division_by_zero THEN
    RAISE NOTICE 'Division by zero caught';
END $$;
AThe exception name 'division_by_zero' is incorrect; it should be 'division_by_zero_error'
BThe exception is not caught because the error is raised outside a BEGIN block
CThe exception name should be SQLSTATE code '22012' instead of the name
DThe exception name is correct and will catch the error
Attempts:
2 left
💡 Hint
Check the exact exception name or SQLSTATE code for division by zero in PostgreSQL.
🧠 Conceptual
expert
3:00remaining
What happens when an exception is re-raised inside an EXCEPTION block?
Consider this PL/pgSQL block. What will be the final output?
PostgreSQL
DO $$
BEGIN
  BEGIN
    RAISE EXCEPTION 'Initial error';
  EXCEPTION
    WHEN OTHERS THEN
      RAISE NOTICE 'Caught error';
      RAISE;
  END;
EXCEPTION
  WHEN OTHERS THEN
    RAISE NOTICE 'Outer handler caught error';
END $$;
AOnly 'Outer handler caught error' notice will be shown
BOnly 'Caught error' notice will be shown
CThe output will be two notices: 'Caught error' and 'Outer handler caught error'
DNo notices will be shown because the exception is unhandled
Attempts:
2 left
💡 Hint
Think about how re-raising an exception propagates it to outer blocks.