在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

这是什么原因呢?

解决方案 »

  1.   

    在使用的过程中,你可能会遇到了下面的问题。 ORA-21000: error number argument to raise_application_error of [xxxx] is out of range 
    出现该异常的原因是:
    Oracle数据库允许自定义的错误代码的范围是-20000到-20999 
      

  2.   


    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
      

  3.   

    呵呵,这个就不是很清楚了。不过不建议你使用oracle自己已经被占用的异常码,有时出现一些问题自己还不知道。按文档的规定来最好。
    指出你上面过程的一个问题:你根本就没有地方抛出你自定义的异常。所以你在exception中不可能捕获
    你的自定义异常。
      

  4.   

    可能是我没说清楚,01403这个错误是select into没有返回行。我在语句块的执行部分写的这句话select id into v from student where name='FEW';是在student表中查找FEW这个人,而我的数据库表中没有这个人,所以系统会抛出select into无返回行错误。
      

  5.   

    请问zhangcunhua,如果不符或异常,系统提示:
    第 1 行出现错误:
    ORA-01403: 未找到数据
    ORA-06512: 在 line 6

    错误编号应该是-01403呀,为什么是100呢?请不吝赐教
      

  6.   

    这个错误也就是no_data_found 
    你看一下我发的那个链接,
    那里面no_data_found   对应的sqlcode是100