1.如何抛出具体哪一行的错误,在oracle里面,可以用dbms_utility.format_error_backtrace打印具体的错误行号,
在pgsql里面,怎么打印呢?
do
$$
declare
v_num int ;
v_text1 text;
v_text2 text;
begin
v_num:=1/0;
exception when others
then get stacked diagnostics v_text1= MESSAGE_TEXT,
v_text2=PG_EXCEPTION_CONTEXT;
--raise notice '异常:%,%',sqlstate ,sqlerrm;
raise notice '异常:%,%',v_text1,v_text2;
end;
$$;
--其中,PG_EXCEPTION_CONTEXT会报告具体的错误行;排查错误很方便;
这里主要是参考了:
https://yq.aliyun.com/articles/700347?do=login&accounttraceid=3b09f1d6-1f87-47d1-939b-6e71f915b3bd
GET STACKED DIAGNOSTICS捕获异常时的STACK内容
GET STACKED DIAGNOSTICS … PG_EXCEPTION_CONTEXT returns the same sort of stack trace, but describing the location at which an error was detected, rather than the current location.
2.如何自定义异常
那么如何自定义异常?
RAISE level 'format' [, expression [, ...]];
这里包含的级别有DEBUG(向服务器日志写信息)、LOG(向服务器日志写信息,优先级更高)、
INFO、NOTICE和WARNING(把信息写到服务器日志以及转发到客户端应用,优先级逐步升高)和
EXCEPTION抛出一个错误(通常退出当前事务)。某个优先级别的信息是报告给客户端还是写到服务器日志,
还是两个均有,是由log_min_messages和client_min_messages这两个系统初始化参数控制的。*/
do
$$
declare
begin
if 1=1 then
raise exception '错误了1';
else
raise exception '错误了2';
end if;
exception when others then
raise notice '错误信息:%',SQLERRM;
end;
$$;
参考:
https://www.cnblogs.com/lottu/p/7410978.html
http://www.postgresql.org/docs/9.5/static/plpgsql-errors-and-messages.html