1-- Simple exception:
2RAISE EXCEPTION 'This user role does not exist.';
3
4-- This example will abort the transaction with the given error message and
5-- hint:
6RAISE EXCEPTION 'Nonexistent ID --> %', user_id
7 USING HINT = 'Please check your user ID';
8
9-- These two examples show equivalent ways of setting the SQLSTATE:
10RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation';
11RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
12
13-- There is a second RAISE syntax in which the main argument is the condition
14-- name or SQLSTATE to be reported, for example:
15RAISE division_by_zero;
16RAISE SQLSTATE '22012';
17
18-- In this syntax, USING can be used to supply a custom error message, detail,
19-- or hint. Another way to do the earlier example is
20RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;