0
0
PostgreSQLquery~10 mins

Exception handling (BEGIN-EXCEPTION-END) in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exception handling (BEGIN-EXCEPTION-END)
Start BEGIN block
Execute statements
Error occurs?
NoCommit and End
Yes
Go to EXCEPTION block
Handle error
End block
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
StepActionEvaluationResult
1Enter BEGIN blockNo error yetProceed
2Execute RAISE NOTICE 'Start'Print messageOutput: Start
3Execute PERFORM 1/0Division by zero errorError raised, jump to EXCEPTION
4Enter EXCEPTION blockCheck error typeMatches division_by_zero
5Execute RAISE NOTICE 'Caught division by zero'Print messageOutput: Caught division by zero
6End blockNo further codeExit successfully
💡 Execution stops after handling the division_by_zero exception and completing the block.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
Error stateNoneNonedivision_by_zero error raisedHandledNone
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.