Oracle异常处理

                                                             异常处理的种类

 

   PL/SQL有三种方式抛出异常:

(1)    对于Oracle系统预定义异常,PL/SQL运行时抛出。

(2)    对于自定义异常,用户使用RAISE语句抛出。

(3)    Oracle还为异常定义了错误代码和错误信息,因此用户还可以通过调用raise_application_error抛出异常。

 

 

1.       预定义异常

 

       异常名字

        描述

 SQLCODE

SQLERRM

    access_into_null

赋值给尚未初始化的对象

-6530

ORA-06530

    case_not_found

case语句中,没有when

-6592

ORA-06592

    collection_is_null

集合为空

-6531

ORA-06531

    cursor_already_open

尝试打开一个已打开的游标

-6511

ORA-06511

    dup_val_on_index

尝试把复制值存入唯一约束列

-1

ORA-00001

    invalid_cursor

关闭一个没有打开的游标

-1001

ORA-01001

    invalid_number

换算或者舍位或者舍入数值无效

-1722

ORA-01722

    login_denied

尝试用无效用户名和密码登录Oracle

-1017

ORA-01017

    no_data_found

在提取时候没有找到数据

+100

ORA-01403

    not_logged_on

没有连接到OraclePL/SQL发起数据库调用

-1012

ORA-1012

    program_error

PL/SQL内部错误

-6501

ORA-06501

    rowtype_mismatch

记录类型不匹配

-6504

ORA-06504

    self_is_null

对象为空

-30625

ORA-30625

    storage_error

PL/SQL程序运行时内存溢出

-6500

ORA-06500

    subscript_beyond_count

下标越界

-6533

ORA-06533

    subscript_outside_limit

下标不合逻辑取值

-6532

ORA-06532

    sys_invalid_rowid

无效rowid

-1410

ORA-01410

    timeout_on_resource

Oracle在等待对策时超时

-51

ORA-00051

    too_many_rows

在提取时,返回行数超过一行

-1422

ORA-01422

    zero_divide

0做除数

-6502

ORA-06502

    value_error

变量长度不够

-1476

ORA-01476

 

例:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.       自定义异常

 

语法:

declare

exception_name exception;

begin

statements;

raise<exception_name>

exception

when<exception_name>then

end;

 

 

例:

declare

  v_empno emp.empno%type:=&v_empno;

  v_emp emp%rowtype;

  empno_out_of_range exception;

begin

  if v_empno<=7000 or v_empno>=8000 then

     raise empno_out_of_range;

  end if;

 

  select * into v_emp from emp where empno=v_empno;

  if SQL%found then

     dbms_output.put_line(v_emp.empno);

     dbms_output.put_line(v_emp.ename);

  end if;

  exception

     when empno_out_of_range then

        dbms_output.put_line('empno is out of range');

     when no_data_found then

        dbms_output.put_line('not found record');

     when too_many_rows then

        dbms_output.put_line('too many record');

end;

 

 

-------输入值:7000

---------------------------------执行结果-------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.       SQLCODESQLERRM

 

例:查看异常too_many_rowsSQLCODESQLERRM

 

declare

  v_emp emp%rowtype;

  empno_out_of_range exception;

begin

  select * into v_emp from emp;

  if SQL%found then

     dbms_output.put_line(v_emp.empno);

     dbms_output.put_line(v_emp.ename);

  end if;

  exception

     when empno_out_of_range then

        dbms_output.put_line('empno is out of range');

     when no_data_found then

        dbms_output.put_line('not found record');

     when too_many_rows then

        dbms_output.put_line(SQLCODE||','||SQLERRM);

end;

 

 

--------------------------------执行结果-----------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1.       给自定义错误标注号码

 

      raise_application_error内建函数用于抛出一个异常并给异常赋予一个错误号以及错误信息。自定义异常的默认错误号是+1,默认信息是user_defined_exceptionraise_application_error函数能够在PL/SQL程序块的执行部分和异常部分调用,显示抛出带特殊错误号的命名异常。它的语法形式如下:

raise_application_error(error_name,error_message);

 

错误号的范围是-20000~-20999。错误信息是文本字符串,最多为2048字节。

例:

 

declare

  v_empno emp.empno%type:=&v_empno;

  v_emp emp%rowtype;

  empno_out_of_range exception;

begin

  if v_empno<=7000 or v_empno>=8000 then

     raise empno_out_of_range;

  end if;

 

  select * into v_emp from emp where empno=v_empno;

  if SQL%found then

     dbms_output.put_line(v_emp.empno);

     dbms_output.put_line(v_emp.ename);

  end if;

  exception

     when empno_out_of_range then

         raise_application_error(-20010,'empno is out of range');

     when no_data_found then

        dbms_output.put_line('not found record');

     when too_many_rows then

        dbms_output.put_line('too many record');

end;

 

---输入值:7000

---------------------------------执行结果---------------------------------------

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值