Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions

From: mike davis <mike(dot)davis65(at)hotmail(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Cc: "\"tgl(at)sss(dot)pgh(dot)pa(dot)us; pavel(dot)stehule\"(at)gmail(dot)com" <"tgl(at)sss(dot)pgh(dot)pa(dot)us; pavel(dot)stehule"(at)gmail(dot)com>
Subject: Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions
Date: 2017-09-22 11:58:18
Message-ID: HE1P190MB0395EB5B479AA5451A01C00B81670@HE1P190MB0395.EURP190.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>This works for me:
>
>DO $$
>DECLARE
> v_msg TEXT := 'SOMETHING IS WRONG';
> v_sqlstate TEXT := 'E0001';
>BEGIN
> RAISE EXCEPTION USING message = v_msg, errcode = v_sqlstate;
>EXCEPTION
> WHEN SQLSTATE 'E0001' THEN
> RAISE NOTICE '%','Error E0001 raised - going to do something about it';
> WHEN OTHERS THEN
> RAISE NOTICE 'OTHER ERRORS: %,%', sqlstate,sqlerrm;
>END$$;

>NOTICE: Error E0001 raised - going to do something about it
>
>Or you could do
> RAISE EXCEPTION SQLSTATE v_sqlstate USING message = v_msg;

That does indeed work !

The second possible way of :
RAISE EXCEPTION SQLSTATE v_sqlstate USING message = v_msg;

however doesn't ! I think that was the format i had also tried and why i went down the dymanic route.

So it seems variables can be used in the USING subclause but not outside it. The manual does seem to hint at this as
"after level if any, you can write a format (which must be a simple string literal, not an expression)"

Anyway, RAISE EXCEPTION USING message = v_msg, errcode = v_sqlstate; works a treat!

Many thanks Tom & Pavel.

Mike

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Imre Samu 2017-09-22 13:22:01 Re: Performance appending to an array column
Previous Message Vladimir Mihailenco 2017-09-22 11:28:38 shared_buffers smaller than max_wal_size