在pl/sql 中,可以使用系统非预定义的异常,但是,我发现,绑定别的编号的异常都可以执行,例如00001(数据唯一性冲突异常)等等,唯独01403异常(select into 没有找到数据异常)不行,系统会报错:
SQL> declare
2 myexception exception;
3 pragma exception_init(myexception,-01403);
4 v number;
5 begin
6 select id into v from student where name='FEW';
7 exception
8 when myexception then
9 dbms_output.put_line('成功俘获异常!');
10 when others then
11 dbms_output.put_line('其他异常!');
12 end;
13 /
myexception exception;
*
第 2 行出现错误:
ORA-06550: 第 2 行, 第 1 列:
PLS-00701: PRAGMA EXCEPTION_INIT 的非法 ORACLE 错误号 -1403
这是什么原因呢?
SQL> declare
2 myexception exception;
3 pragma exception_init(myexception,-01403);
4 v number;
5 begin
6 select id into v from student where name='FEW';
7 exception
8 when myexception then
9 dbms_output.put_line('成功俘获异常!');
10 when others then
11 dbms_output.put_line('其他异常!');
12 end;
13 /
myexception exception;
*
第 2 行出现错误:
ORA-06550: 第 2 行, 第 1 列:
PLS-00701: PRAGMA EXCEPTION_INIT 的非法 ORACLE 错误号 -1403
这是什么原因呢?
出现该异常的原因是:
Oracle数据库允许自定义的错误代码的范围是-20000到-20999
Declare
Myexception Exception;
Pragma Exception_Init(Myexception, 100);
v Number;
Begin
Select Id Into v From Student Where Name = 'FEW';
Exception
When Myexception Then
Dbms_Output.Put_Line('成功俘获异常!');
When Others Then
Dbms_Output.Put_Line('其他异常!');
End;
这个错误和sqlcode是不一致的,其实是+100
相关链接
http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96624/07_errs.htm#784
指出你上面过程的一个问题:你根本就没有地方抛出你自定义的异常。所以你在exception中不可能捕获
你的自定义异常。
第 1 行出现错误:
ORA-01403: 未找到数据
ORA-06512: 在 line 6
错误编号应该是-01403呀,为什么是100呢?请不吝赐教
你看一下我发的那个链接,
那里面no_data_found 对应的sqlcode是100