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

[ &lt;&lt;label&gt;&gt; ]
[ 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.