Re: User defined exceptions - Mailing list pgsql-sql

From Alexey Bashtanov
Subject Re: User defined exceptions
Date
Msg-id 55A8AFF3.7040305@ocslab.com
Whole thread Raw
In response to User defined exceptions  (Alex Ignatov <a.ignatov@postgrespro.ru>)
List pgsql-sql
<div class="moz-cite-prefix">On 15.07.2015 17:10, Alex Ignatov wrote:<br /></div><blockquote
cite="mid:%3C55A669CA.3070302@postgrespro.ru%3E"type="cite"> Hello all!<br /> Trying to emulate "named" user defined
exceptionwith:<br /> CREATE OR REPLACE FUNCTION exception_aaa ()  RETURNS text AS $body$<br /> BEGIN<br />    return
31234;     <br /> END;<br /> $body$<br /> LANGUAGE PLPGSQL<br /> SECURITY DEFINER<br /> ;<br /><br /> do $$<br />
begin<br/>    raise exception using errcode=exception_aaa();<br /> exception <br />    when  sqlstate
exception_aaa()<br/>    then<br />       raise notice 'got exception %',sqlstate;<br /> end;<br /> $$<br />  <br />
Got:<br/><br /> ERROR:  syntax error at or near "exception_aaa"<br /> LINE 20: sqlstate exception_aaa()<br /><br /> I
lookslike "when  sqlstate exception_aaa()" doesn't work. <br /><br /> How can I catch exception in this case?<br
/></blockquote><br/> Hello Alex,<br /><br /> The following workaround could be used:<br /><br /> do $$<br /> begin<br
/>   raise exception using errcode = exception_aaa();<br /> exception<br />    when others then<br />       if sqlstate
=exception_aaa() then<br />          raise notice 'got exception %',sqlstate;<br />       else<br />          raise;
--reraise<br/>       end if;<br /> end;<br /> $$<br /><br /> Not sure if its performance is the same as in simple
exceptioncatch, maybe it would degrade.<br /><br /> Best Regards,<br />    Alexey Bashtanov<br /> 

pgsql-sql by date:

Previous
From: Alex Ignatov
Date:
Subject: Re: User defined exceptions
Next
From: Mario Splivalo
Date:
Subject: Getting the list of foreign keys (for deleting data from the database)