Re: User defined exceptions

From: Alex Ignatov <a(dot)ignatov(at)postgrespro(dot)ru>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: User defined exceptions
Date: 2015-07-17 15:31:39
Message-ID: 55A91FDB.1010706@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 17.07.2015 10:34, Alexey Bashtanov wrote:
> On 15.07.2015 17:10, Alex Ignatov wrote:
>> Hello all!
>> Trying to emulate "named" user defined exception with:
>> CREATE OR REPLACE FUNCTION exception_aaa () RETURNS text AS $body$
>> BEGIN
>> return 31234;
>> END;
>> $body$
>> LANGUAGE PLPGSQL
>> SECURITY DEFINER
>> ;
>>
>> do $$
>> begin
>> raise exception using errcode=exception_aaa();
>> exception
>> when sqlstate exception_aaa()
>> then
>> raise notice 'got exception %',sqlstate;
>> end;
>> $$
>>
>> Got:
>>
>> ERROR: syntax error at or near "exception_aaa"
>> LINE 20: sqlstate exception_aaa()
>>
>> I looks like "when sqlstate exception_aaa()" doesn't work.
>>
>> How can I catch exception in this case?
>
> Hello Alex,
>
> The following workaround could be used:
>
> do $$
> begin
> raise exception using errcode = exception_aaa();
> exception
> when others then
> if sqlstate = exception_aaa() then
> raise notice 'got exception %',sqlstate;
> else
> raise; --reraise
> end if;
> end;
> $$
>
> Not sure if its performance is the same as in simple exception catch,
> maybe it would degrade.
>
> Best Regards,
> Alexey Bashtanov
Yep already used this trick =)
Anyway thank you!

--
Alex Ignatov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mario Splivalo 2015-08-02 15:04:32 Getting the list of foreign keys (for deleting data from the database)
Previous Message Alexey Bashtanov 2015-07-17 07:36:55 Re: User defined exceptions