custom exceptions

suggest change

Creating a custom exception with SQLSTATE P2222:

create or replace function s164() returns void as
$$
begin
raise exception using message = 'S 164', detail = 'D 164', hint = 'H 164', errcode = 'P2222';
end;
$$ language plpgsql
;

Creating an exception without errcode. It defaults to P0001 (raise_exception):

create or replace function s165() returns void as
$$
begin
raise exception '%','nothing specified';
end;
$$ language plpgsql
;

calling:

t=# do
$$
declare
 _t text;
begin
  perform s165();
  exception when SQLSTATE 'P0001' then raise info '%','state P0001 caught: '||SQLERRM;
  perform s164();

end;
$$
;
INFO:  state P0001 caught: nothing specified
ERROR:  S 164
DETAIL:  D 164
HINT:  H 164
CONTEXT:  SQL statement "SELECT s164()"
PL/pgSQL function inline_code_block line 7 at PERFORM

The handler catches P0001. P2222 is not caught, so that error aborts execution.

It helps to keep a table of the exception codes you use, as in http://stackoverflow.com/a/2700312/5315974

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents