Errors and Messages

1. Reporting Errors and Messages

Use the RAISE statement to report messages and raise errors.

RAISE [ level ] ’format’ [, expression [, ... ]] [ USING option = expression [, ... ] ];
RAISE [ level ] condition_name [ USING option = expression [, ... ] ];
RAISE [ level ] SQLSTATE ’sqlstate’ [ USING option = expression [, ... ] ];
RAISE [ level ] USING option = expression [, ... ];
RAISE ;

The level option specifies the error severity. Allowed levels are DEBUG, LOG, INFO, NOTICE, WARNING, and EXCEPTION, with EXCEPTION being the default.

EXCEPTION raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels.

Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the log_min_messages and client_min_messages configuration variables.

After level if any, you can write a format (which must be a simple string literal, not an expression).

The format string specifies the error message text to be reported. The format string can be followed by optional argument expressions to be inserted into the message.

Inside the format string, % is replaced by the string representation of the next optional argument’s value.

Write %% to emit a literal %. The number of arguments must match the number of % placeholders in the format string, or an error is raised during the compilation of the function.

In this example, the value of v_job_id will replace the % in the string:

RAISE NOTICE ’Calling cs_create_job(%)’, v_job_id;

You can attach additional information to the error report by writing USING followed by option = expression items. Each expression can be any string-valued expression.

The allowed option key words are:

MESSAGE

Sets the error message text. This option can’t be used in the form of RAISE that includes a format

string before USING.

DETAIL

Supplies an error detail message.

HINT

Supplies a hint message.

ERRCODE

Specifies the error code (SQLSTATE) to report, either by condition name or directly as a five-character SQLSTATE code.

COLUMN
CONSTRAINT
DATATYPE
TABLE
SCHEMA

Supplies the name of a related object.

This example will abort the transaction with the given error message and hint:

RAISE EXCEPTION ’Nonexistent ID --> %’, user_id
USING HINT = ’Please check your user ID’;

These two examples show equivalent ways of setting the SQLSTATE:

RAISE ’Duplicate user ID: %’, user_id USING ERRCODE = ’unique_violation’;
RAISE ’Duplicate user ID: %’, user_id USING ERRCODE = ’23505’;

There is a second RAISE syntax in which the main argument is the condition name or SQLSTATE to be reported, for example:

RAISE division_by_zero;
RAISE SQLSTATE ’22012’;

In this syntax, USING can be used to supply a custom error message, detail, or hint.

Another way to do the earlier example is

RAISE unique_violation USING MESSAGE = ’Duplicate user ID: ’ || user_id;

Still another variant is to write RAISE USING or RAISE level USING and put everything else into the USING list.

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

If no condition name nor SQLSTATE is specified in a RAISE EXCEPTION command, the default is to use RAISE_EXCEPTION (P0001).

If no message text is specified, the default is to use the condition name or SQLSTATE as message text.

Note: 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 and/or upper-case ASCII letters, other than 00000.

It is recommended that you avoid throwing error codes that end in three zeroes, because these are category codes and can only be trapped by trapping the whole category.

2. Checking Assertions

The ASSERT statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions.

ASSERT condition [ , message ];

The condition is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT statement does nothing further. If the result is false or null, then an ASSERT_FAILURE exception is raised. (If an error occurs while evaluating the condition, it is reported as a normal error.)

If the optional message is provided, it is an expression whose result (if not null) replaces the default error message text “assertion failed”, should the condition fail. The message expression is not evaluated in the normal case where the assertion succeeds.

Testing of assertions can be enabled or disabled via the configuration parameter plpgsql.check_asserts, which takes a Boolean value; the default is on. If this parameter is off then ASSERT statements do nothing.

Note that ASSERT is meant for detecting program bugs, not for reporting ordinary error conditions. Use the RAISE statement, described above, for that

最新文章

  1. Python与PHP通过XMLRPC进行通信
  2. linux开机启动程序
  3. 在Windows上,迁移VisualSVN server
  4. Swift类与结构体
  5. linux 下ffmpeg和mencoder安装
  6. ecshop模板如何修改详细图解
  7. Java日志终极指南
  8. 限制div高度当内容多了溢出时显示滚动条
  9. zoj3791(An Easy Game) DP
  10. Python通过百度Ai识别图片中的文字
  11. 微信小程序-form表单-获取用户输入文本框的值
  12. zabbix-2.4.5的安装配置与使用
  13. 【洛谷P4462】异或序列
  14. KVM虚拟化研究-1
  15. 【MySql 】is not allowed to connect to this MySql server 无法访问远程MySQL数据库
  16. mac设置java环境变量, 使用oh-my-zsh
  17. extjs fileuploadfield default value
  18. 基于python的接口测试框架设计(一)连接数据库
  19. 双系统(Windows+Ubuntu)重装Ubuntu后,修复引导
  20. Kubernetes对象模型

热门文章

  1. linux中memset的正确用法
  2. Idea搭建Scala开发环境的注意事项
  3. LINQ GroupBy 查询数据赋给select
  4. sql server2008 跨服务器之间复制表数据
  5. c++ 组合模式(composite)
  6. Extend volumn in ubuntu 14.04
  7. 基于 EntityFramework 的数据库主从读写分离架构(1) - 原理概述和基本功能实现
  8. Linux扩展根目录
  9. getopt两个模块getopt 和gun_getopt 的异同
  10. React官方网站学习