The BEGIN block runs code; if an error happens, control jumps to EXCEPTION block to handle it, then ends.
Execution Sample
PostgreSQL
DO $$
BEGIN
RAISE NOTICE 'Start';
PERFORM 1/0; -- causes error
EXCEPTION
WHEN division_by_zero THEN
RAISE NOTICE 'Caught division by zero';
END $$;
This code tries to divide by zero, catches the error, and prints a message instead of stopping.
Execution Table
Step
Action
Evaluation
Result
1
Enter BEGIN block
No error yet
Proceed
2
Execute RAISE NOTICE 'Start'
Print message
Output: Start
3
Execute PERFORM 1/0
Division by zero error
Error raised, jump to EXCEPTION
4
Enter EXCEPTION block
Check error type
Matches division_by_zero
5
Execute RAISE NOTICE 'Caught division by zero'
Print message
Output: Caught division by zero
6
End block
No further code
Exit successfully
💡 Execution stops after handling the division_by_zero exception and completing the block.
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 5
Final
Error state
None
None
division_by_zero error raised
Handled
None
Key Moments - 3 Insights
Why does the code jump to the EXCEPTION block after the error?
Because the error 'division_by_zero' occurs at step 3, PostgreSQL immediately transfers control to the EXCEPTION block to handle it, as shown in execution_table row 3 and 4.
What happens if the error type does not match any WHEN clause?
If no matching WHEN clause is found, the error is not handled inside the block and will propagate outside, causing the block to fail. This is why matching error types in EXCEPTION is important.
Does the code after the error inside BEGIN block run?
No, once an error occurs at step 3, the remaining code in BEGIN block is skipped and control moves to EXCEPTION block, as shown in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 2?
AStart
BCaught division by zero
CNo output
DError message
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the error occur that triggers the EXCEPTION block?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the step where 'division_by_zero error' is raised in the execution_table.
If the error was not division_by_zero but another error, what would happen?
AThe block would ignore the error and continue
BThe error would propagate outside and stop execution
CThe EXCEPTION block would handle it anyway
DThe error would be silently ignored
💡 Hint
Refer to key_moments about unmatched error types in EXCEPTION block.
Concept Snapshot
BEGIN-EXCEPTION-END block in PostgreSQL:
- BEGIN runs code normally.
- If error occurs, jump to EXCEPTION.
- EXCEPTION handles specific errors with WHEN clauses.
- After handling, block ends gracefully.
- Unhandled errors propagate outside.
Full Transcript
In PostgreSQL, exception handling uses a BEGIN block to run code. If an error happens, control jumps to the EXCEPTION block where you can handle specific errors using WHEN clauses. For example, dividing by zero causes an error that is caught and handled by printing a message. The code after the error inside BEGIN does not run. If the error type is not matched, the error propagates outside and stops execution. This mechanism helps keep your database code safe and predictable.