Re: user's exception PL/pgSQL

From: Pavel Stehule <stehule(at)kix(dot)fsv(dot)cvut(dot)cz>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, <pgsql-patches(at)postgresql(dot)org>
Subject: Re: user's exception PL/pgSQL
Date: 2005-06-15 12:31:10
Message-ID: Pine.LNX.4.44.0506151344100.5009-200000@kix.fsv.cvut.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

Hello,

the name of exception's variable I use as exception's name. Attached patch
work, but miss documentations, regress, ..

>BTW, is there any value in a separate "EXCEPTION" type? ISTM that an
>exception is just a SQLSTATE, which is in turn just a string. A separate
>exception type does simplify the parsing of RAISE, but I wonder if it
>would be useful to be able to also allow specifying the SQLSTATE code as
>a string literal.

Definition new attributes for exception isn't problem: level, errmsg.
Parsing raise stmt will be little bit more complicete. But why now?

CREATE OR REPLACE FUNCTION foo() RETURNS void AS $$
DECLARE my_own_exception EXCEPTION;
BEGIN
RAISE EXCEPTION my_own_exception 'some text';
END; $$ LANGUAGE plpgsql;

pokus=# select foo();
ERROR: some text
DETAIL: User's exception sqlstate: U0001, name: my_own_exception
HINT: Unhandled user's exception, from RAISE stmt on line 3
pokus=#

CREATE OR REPLACE FUNCTION foo() RETURNS void AS $$
DECLARE div_by_zero_test EXCEPTION = '22012';
BEGIN
RAISE EXCEPTION div_by_zero_test 'some text';
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'foo text';
END; $$ LANGUAGE plpgsql;
pokus=# select foo();
NOTICE: foo text
foo
-----

(1 row)

CREATE OR REPLACE FUNCTION foo() RETURNS void AS $$
DECLARE uexcpt01 EXCEPTION;
BEGIN
RAISE EXCEPTION uexcpt01 'aaaa';
EXCEPTION WHEN uexcpt01 THEN
RAISE NOTICE 'hello';
END; $$ LANGUAGE plpgsql;
pokus=# select foo();
NOTICE: hello

The patch isn't in production state (no from me:), but maybe is usefull
for test. The moust important is posibility handling own exception without
parsing SQLERRMS, I think. Setting SQLSTATE is usefull for
interoperatibility between procedures and throwing system errors.

Regards
Pavel Stehule

Attachment Content-Type Size
usrexcpt.diff text/plain 17.7 KB

In response to

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2005-06-15 12:52:51 Re: uptime function to postmaster
Previous Message Neil Conway 2005-06-15 07:47:27 Re: hash join: probe both inputs first