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?
✗ Incorrect
The BEGIN keyword starts the block where you write code that might raise exceptions.
Which keyword is used to catch errors in PostgreSQL exception handling?
✗ Incorrect
Inside the EXCEPTION block, WHEN is used to specify which errors to catch.
What happens if an error is not caught inside a BEGIN-EXCEPTION-END block?
✗ Incorrect
If an error is not caught, it propagates and usually stops the transaction.
How do you handle a division by zero error in PostgreSQL exception block?
✗ Incorrect
The correct error name is division_by_zero.
Can you re-raise an error inside the EXCEPTION block?
✗ Incorrect
You can re-raise errors using the RAISE statement.
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.