Debugging

Syntax

RAISE has the following five syntax formats:

Figure 1 raise_format::=
Figure 2 raise_condition::=
Figure 3 raise_sqlstate::=
Figure 4 raise_option::=
Figure 5 raise::=

Parameter description:

If neither a condition name nor an SQLSTATE is designated in a RAISE EXCEPTION command, the RAISE EXCEPTION (P0001) is used by default. If no message text is designated, the condition name or SQLSTATE is used as the message text by default.

If the SQLSTATE designates an error code, the error code is not limited to a defined error code. It can be any error code containing five digits or ASCII uppercase rather than 00000. Do not use an error code ended with three zeros because this kind of error codes are type codes and can be captured by the whole category.

The syntax described in Figure 5 does not append any parameter. This form is used only for the EXCEPTION statement in a BEGIN block so that the error can be re-processed.

Examples

Display error and hint information when a transaction terminates:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
CREATE OR REPLACE PROCEDURE proc_raise1(user_id in integer)
AS 
BEGIN 
RAISE EXCEPTION 'Noexistence ID --> %',user_id USING HINT = 'Please check your user ID'; 
END; 
/

call proc_raise1(300011);

-- Execution result:
ERROR:  Noexistence ID --> 300011
HINT:  Please check your user ID
Two methods are available for setting SQLSTATE:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
CREATE OR REPLACE PROCEDURE proc_raise2(user_id in integer)
AS 
BEGIN 
RAISE 'Duplicate user ID: %',user_id USING ERRCODE = 'unique_violation'; 
END; 
/

\set VERBOSITY verbose
call proc_raise2(300011);

-- Execution result:
ERROR:  Duplicate user ID: 300011
SQLSTATE: 23505
LOCATION:  exec_stmt_raise, pl_exec.cpp:3482

If the main parameter is a condition name or SQLSTATE, the following applies:

RAISE division_by_zero;

RAISE SQLSTATE '22012';

For example:

CREATE OR REPLACE PROCEDURE division(div in integer, dividend in integer) 
AS 
DECLARE 
res int; 
    BEGIN 
    IF dividend=0 THEN 
        RAISE division_by_zero; 
        RETURN; 
    ELSE 
        res := div/dividend; 
        RAISE INFO 'division result: %', res;
        RETURN; 
    END IF; 
    END; 
/
call division(3,0);

-- Execution result:
ERROR:  division_by_zero
Alternatively:
1
RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;