PostgreSQL自定义异常? [英] PostgreSQL custom exceptions?
本文介绍了PostgreSQL自定义异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Firebird中,我们可以声明自定义异常:
In Firebird we can declare custom exceptions like so:
CREATE EXCEPTION EXP_CUSTOM_0异常:自定义异常;
CREATE EXCEPTION EXP_CUSTOM_0 'Exception: Custom exception';
这些都存储在数据库级。在存储过程中,我们可以像这样引发异常:
these are stored at the database level. In stored procedures, we can raise the exception like so:
EXCEPTION EXP_CUSTOM_0;
EXCEPTION EXP_CUSTOM_0 ;
PostgreSQL中有一个等价物吗?
Is there an equivalent in PostgreSQL ?
推荐答案
不,不是这样。但是你可以提高和维护自己的例外,没有问题:
No, not like this. But you can raise and maintain your own exceptions, no problem:
CREATE TABLE exceptions(
id serial primary key,
MESSAGE text,
DETAIL text,
HINT text,
ERRCODE text
);
INSERT INTO exceptions (message, detail, hint, errcode) VALUES ('wrong', 'really wrong!', 'fix this problem', 'P0000');
CREATE OR REPLACE FUNCTION foo() RETURNS int LANGUAGE plpgsql AS
$$
DECLARE
row record;
BEGIN
PERFORM * FROM fox; -- does not exist, undefined_table, fail
EXCEPTION
WHEN undefined_table THEN
SELECT * INTO row FROM exceptions WHERE id = 1; -- get your exception
RAISE EXCEPTION USING MESSAGE = row.message, DETAIL = row.detail, HINT = row.hint, ERRCODE = row.errcode;
RETURN 1;
END;
$$
SELECT foo();
您也可以在您的程序中使用硬编码,这取决于您。
Offcourse you can also have them hardcoded in your procedures, that's up to you.
这篇关于PostgreSQL自定义异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文