0
0
PostgreSQLquery~5 mins

Exception handling (BEGIN-EXCEPTION-END) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the BEGIN-EXCEPTION-END block in PostgreSQL?
It is used to handle errors gracefully by running code that might fail inside BEGIN, catching errors in EXCEPTION, and then continuing or cleaning up in END.
Click to reveal answer
intermediate
How do you catch a specific error in a PostgreSQL exception block?
Use WHEN followed by the error name or SQLSTATE code inside the EXCEPTION block to catch specific errors.
Click to reveal answer
beginner
What happens if an error occurs inside a BEGIN block but there is no EXCEPTION block?
The error will stop the transaction and the error will be returned to the caller without any special handling.
Click to reveal answer
intermediate
Write a simple example of a BEGIN-EXCEPTION-END block that catches division by zero error.
BEGIN PERFORM 1 / 0; EXCEPTION WHEN division_by_zero THEN RAISE NOTICE 'Cannot divide by zero'; END;
Click to reveal answer
intermediate
Can you re-raise an error inside the EXCEPTION block in PostgreSQL?
Yes, you can use RAISE to re-throw the error after handling or logging it.
Click to reveal answer
What keyword starts the block where you handle exceptions in PostgreSQL?
ABEGIN
BEXCEPTION
CTRY
DCATCH
Which keyword is used to catch errors in PostgreSQL exception handling?
ACATCH
BEXCEPTION
CHANDLE
DWHEN
What happens if an error is not caught inside a BEGIN-EXCEPTION-END block?
AThe transaction continues normally
BThe error is propagated and stops the transaction
CThe error is ignored
DThe error is automatically fixed
How do you handle a division by zero error in PostgreSQL exception block?
AWHEN div_zero THEN
BWHEN zero_division THEN
CWHEN division_by_zero THEN
DWHEN divide_by_zero THEN
Can you re-raise an error inside the EXCEPTION block?
AYes, using RAISE
BYes, using THROW
CNo, errors cannot be re-raised
DOnly in PL/pgSQL functions
Explain how the BEGIN-EXCEPTION-END block works in PostgreSQL for error handling.
Think about how you try something, catch problems, and then finish.
You got /5 concepts.
    Describe how to catch and handle a specific error like division by zero in PostgreSQL.
    Focus on the keywords and error name.
    You got /4 concepts.