pragma exception_init(deadlock_detected, -60);exception_init编译指 令,从字面意思上我们能了解一点,即exception的初始化。
pragma exception_init(deadlock_detected, -60);
exception_init编译指 令,从字面意思上我们能了解一点,即exception的初始化。
语法也简单,pragma exception (exception_name, error_number);
大家都有经验,在处理exception 是,当没有已经定义好了的exception name可用时,我们就用others来处理未被捕捉的所有的exception,pl/sql设计者建议大家尽量使用已知的exception name来捕捉,不到最后,尽量不用这个选项,虽然不知道为什么,但估计是跟性能有关,而且编译出来的代码应该会很长。
这个编译指令的引入也为这带来了解决方案。比如,我们要捕捉死锁这个exception怎么办? 系统没有预定义好的exception name可用。这时我们就可以用这个编译指令了。
我们知道ora-60错误是代表死锁的意思,那我们就可以为这个错误号起一个exception name,比如deadlock_detected,语法如下:
declare
deadlock_detected exception;
pragma exception_init(deadlock_detected, -60);
好我们就可以像下面一样来捕捉处理这个死锁的exception了。
exception
when deadlock_detected then
......
这样,,我们就避免了用when others then来捕捉处理这个异常了。
这个编译指令几乎可以用在所有的程序的声明项中,但要注意作用范围,另外要注意的是记住只为一个错误号起一个exception name。
下面附上一段通过sqlerrm函数找出oracle预定义的错误号码及相应的错误信息。这在10g版本通过,在之前的版本可能会报buffer overflow错误,这是由于set serveroutput on默认为2000字节,但在10g中默认是unlimited了。
set serveroutput on
spool errormsg.log
declare
vmsg varchar2(500);
begin
for i in 0..20000 loop
vmsg := sqlerrm(-i);
if instr(vmsg,'not found') = 0 then
dbms_output.put_line(vmsg);
end if;
end loop;
end;
/
spool off
---------------再加一个例子---------------
下面看一个例子
create or replace procedure sp_del_test
(p_itemadmin in mfitem.itemadmin%type, --itemadmin
p_itemcd in mfitem.itemcd%type, --itemcode
p_return out number --输出参数
) is
exp exception;
pragma exception_init(exp, -2292);
begin
delete from mfitem t
where t.itemcd = p_itemcd
and t.itemadmin = p_itemadmin; --这一句会引发-2292异常,有级连删除异常
exception
when exp then
p_return := 9;
rollback;
when others then
rollback;
end sp_del_test;