Home Explore Blog CI



postgresql

51th chunk of `doc/src/sgml/plpgsql.sgml`
d5a23c3b55ef7765365ecf5c04a806c9069b99b5050b13920000000100000fa2
 class="parameter">option</replaceable> key words are:

    <variablelist id="raise-using-options">
     <varlistentry id="raise-using-option-message">
      <term><literal>MESSAGE</literal></term>
      <listitem>
       <para>Sets the error message text.  This option can't be used in the
        first syntax variant, since the message is already supplied.</para>
      </listitem>
     </varlistentry>

     <varlistentry id="raise-using-option-detail">
      <term><literal>DETAIL</literal></term>
      <listitem>
       <para>Supplies an error detail message.</para>
      </listitem>
     </varlistentry>

     <varlistentry id="raise-using-option-hint">
      <term><literal>HINT</literal></term>
      <listitem>
       <para>Supplies a hint message.</para>
      </listitem>
     </varlistentry>

     <varlistentry id="raise-using-option-errcode">
      <term><literal>ERRCODE</literal></term>
      <listitem>
       <para>Specifies the error code (SQLSTATE) to report, either by condition
        name, as shown in <xref linkend="errcodes-appendix"/>, or directly as a
        five-character SQLSTATE code.  This option can't be used in the
        second or third syntax variant, since the error code is already
        supplied.</para>
      </listitem>
     </varlistentry>

     <varlistentry id="raise-using-option-column">
      <term><literal>COLUMN</literal></term>
      <term><literal>CONSTRAINT</literal></term>
      <term><literal>DATATYPE</literal></term>
      <term><literal>TABLE</literal></term>
      <term><literal>SCHEMA</literal></term>
      <listitem>
       <para>Supplies the name of a related object.</para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>

   <para>
    This example will abort the transaction with the given error message
    and hint:
<programlisting>
RAISE EXCEPTION 'Nonexistent ID --> %', user_id
      USING HINT = 'Please check your user ID';
</programlisting>
   </para>

   <para>
    These two examples show equivalent ways of setting the SQLSTATE:
<programlisting>
RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation';
RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
</programlisting>
    Another way to produce the same result is:
<programlisting>
RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
</programlisting>
   </para>

   <para>
    As shown in the fourth syntax variant, it is also possible to
    write <literal>RAISE USING</literal> or <literal>RAISE
    <replaceable class="parameter">level</replaceable> USING</literal> and put
    everything else into the <literal>USING</literal> list.
   </para>

   <para>
    The last variant of <command>RAISE</command> has no parameters at all.
    This form can only be used inside a <literal>BEGIN</literal> block's
    <literal>EXCEPTION</literal> clause;
    it causes the error currently being handled to be re-thrown.
   </para>

   <note>
    <para>
     Before <productname>PostgreSQL</productname> 9.1, <command>RAISE</command> without
     parameters was interpreted as re-throwing the error from the block
     containing the active exception handler.  Thus an <literal>EXCEPTION</literal>
     clause nested within that handler could not catch it, even if the
     <command>RAISE</command> was within the nested <literal>EXCEPTION</literal> clause's
     block. This was deemed surprising as well as being incompatible with
     Oracle's PL/SQL.
    </para>
   </note>

   <para>
    If no condition name nor SQLSTATE is specified in a
    <command>RAISE EXCEPTION</command> command, the default is to use
    <literal>raise_exception</literal> (<literal>P0001</literal>).
    If no message text is specified, the default is to use the condition
    name or SQLSTATE as message text.
   </para>

   <note>
    <para>
     When specifying an error code by SQLSTATE code, you are not
     limited to the predefined error codes, but can select any
     error code consisting of five digits

Title: Detailed Options and Usage of the RAISE Statement
Summary
This section elaborates on the options available with the RAISE statement in PL/pgSQL, specifically focusing on the USING clause. It describes how to use options like MESSAGE, DETAIL, HINT, ERRCODE, COLUMN, CONSTRAINT, DATATYPE, TABLE, and SCHEMA to attach additional information to the error report. It also explains how to re-throw an exception using RAISE without parameters within an EXCEPTION clause. Additionally, it clarifies the default behavior of RAISE EXCEPTION when no condition name or SQLSTATE is specified.