HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMajor

Try catch equivalent in Postgres

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgrescatchequivalenttry

Problem

Do we have a try catch equivalent in Postgres? I have written some user defined functions that are called by trigger. I (don't) want to ignore errors so that flow does not get interrupted.

Solution

The equivalent of TRY-CATCH error handling in PostgreSQL is a block of code in this way:

[ > ]
[ DECLARE
    declarations ]
BEGIN
    statements
EXCEPTION
    WHEN condition [ OR condition ... ] THEN
        handler_statements
    [ WHEN condition [ OR condition ... ] THEN
          handler_statements
      ... ]
END;


Have a look at Postgres docs about Trapping errors

If you want to use it in your functions, keep in mind it can only be used inside PL/pgSQL functions.

Code Snippets

[ <<label>> ]
[ DECLARE
    declarations ]
BEGIN
    statements
EXCEPTION
    WHEN condition [ OR condition ... ] THEN
        handler_statements
    [ WHEN condition [ OR condition ... ] THEN
          handler_statements
      ... ]
END;

Context

StackExchange Database Administrators Q#244328, answer score: 32

Revisions (0)

No revisions yet.