Bird
0
0

What will be the output of this PostgreSQL block?

medium📝 query result Q4 of 15
PostgreSQL - Advanced PL/pgSQL
What will be the output of this PostgreSQL block?
DO $$
BEGIN
  RAISE EXCEPTION 'Fatal error';
EXCEPTION
  WHEN unique_violation THEN
    RAISE NOTICE 'Unique violation caught';
  WHEN OTHERS THEN
    RAISE NOTICE 'General exception caught';
END $$;
ANo output, block completes silently
BNOTICE: Unique violation caught
CFATAL: Fatal error
DNOTICE: General exception caught
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the raised exception

    The block raises a generic exception with message 'Fatal error'.
  2. Step 2: Check exception handlers

    There is a handler for unique_violation and a catch-all handler WHEN OTHERS.
  3. Step 3: Determine which handler runs

    Since the exception is not a unique_violation, the WHEN OTHERS handler executes.
  4. Final Answer:

    NOTICE: General exception caught -> Option D
  5. Quick Check:

    WHEN OTHERS catches all exceptions not matched earlier [OK]
Quick Trick: WHEN OTHERS catches all unmatched exceptions [OK]
Common Mistakes:
  • Assuming unique_violation handler will run for all exceptions
  • Expecting the block to terminate without notice

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes