oracle异常处理明细(个人简单总结)

本文深入探讨了Oracle PL/SQL块中的异常处理机制,包括如何捕获、处理不同类型的异常,以及如何利用sqlcode和sqlerrm函数获取异常代码和错误提示信息。此外,文章还介绍了非预定义Oracle异常的初始化方式、用户自定义异常的实现,以及RAISE_APPLICATION_ERROR函数的作用。
异常处理是针对系统中发生的各种错误所采取的处理措施。
PL/SQL块中的异常处理
Sql代码   收藏代码
  1. exception   
  2.     when first_exception then <handle first exception>  
  3.     when second_exception then <handle second exception>  
  4.     when others then <handle other exception>  
exception 
	when first_exception then <handle first exception>
	when second_exception then <handle second exception>
	when others then <handle other exception>


在异常处理中,用来获取异常代码和完整错误提示信息的两个子系统函数是sqlcode和sqlerrm。
对系统内部异常可通过sqlcode返回一个oracle错误编码号。sqlcode返回的编号除了“ora_01403没发现数据”是正值之外,其他的都是负值。
sqlerrm则返回异常代码相对应的错误信息。对用户定义的异常,sqlcode返回+1而且sqlerrm返回“用户定义异常”。如果没有异常发生,操作正常执行,则sqlcode返回0,sqlerrm返回信息“ora_0000:正常,成功完成”。


非预定义的Oracle异常
pragma exception_init(<exception_name>,<handle_code>)
在PL*SQL中,pragma exception_init告诉编译器将一个Oracle错误编号与异常名建立起来

Sql代码   收藏代码
  1. pragma exception_init的用法  
  2. declare   
  3.  e_emp_remaining EXCEPTION;  
  4.  pragma exception_init(e_emp_remaining ,-2292);  
  5. begin  
  6.  delete from dept where deptno=10;  
  7. commit;  
  8. EXCEPTION   
  9.     when (e_emp_remaining then  
  10.         dbms_output.put_line('cannot remove dept'||to_char(10)||'.employee exist.');  
  11. end;  
pragma exception_init的用法
declare 
 e_emp_remaining EXCEPTION;
 pragma exception_init(e_emp_remaining ,-2292);
begin
 delete from dept where deptno=10;
commit;
EXCEPTION 
	when (e_emp_remaining then
        dbms_output.put_line('cannot remove dept'||to_char(10)||'.employee exist.');
end;


用户自定义异常
Sql代码   收藏代码
  1. declare  
  2.  v_eno emp.empno%type :=&.empno;  
  3.  not_found exception;  
  4. begin  
  5.  update emp set sal=sal*1.1 where  empno=v_eno;  
  6.  if  SQL% notfound then  
  7.   
  8.    raise not_found// 使用RAISE语句抛出异常  
  9.   
  10.  end if;  
  11. exception  
  12.  when not_found then  
  13.       dbms_output.put_line('You can'update the sal,the number does not!exist!');  
  14. when others then  
  15.       dbms_output.put_line('other other');  
  16. end  
declare
 v_eno emp.empno%type :=&.empno;
 not_found exception;
begin
 update emp set sal=sal*1.1 where  empno=v_eno;
 if  SQL% notfound then

   raise not_found// 使用RAISE语句抛出异常

 end if;
exception
 when not_found then
      dbms_output.put_line('You can't update the sal,the number does not!exist!');
when others then
      dbms_output.put_line('other other');
end


RAISE_APPLICATION_ERROR
可能不是很多人知道 RAISE_APPLICATION_ERROR 的用途是什么,虽然从字面上已经猜到这个函数是干什么用的。
      其实 RAISE_APPLICATION_ERROR 是将应用程序专有的错误从服务器端转达到客户端应用程序。

   
Sql代码   收藏代码
  1. RAISE_APPLICATION_ERROR 的声明:  
  2.   
  3.   PROCEDURE RAISE_APPLICATION_ERROR (error_number_in IN NUMBER, error_msg_in IN VARCHAR2);  
  RAISE_APPLICATION_ERROR 的声明:
 
    PROCEDURE RAISE_APPLICATION_ERROR (error_number_in IN NUMBER, error_msg_in IN VARCHAR2);

      里面的错误代码和内容,都是自定义的。说明是自定义,当然就不是系统中已经命名存在的错误类别,是属于一种自定义事务错误类型,才调用此函数。
      error_number_in 之容许从 -20000 到 -20999 之间,这样就不会与 ORACLE 的任何错误代码发生冲突。
      error_msg_in 的长度不能超过 2K,否则截取 2K。


举个例吧:
阻止小于18岁的用户增加到数据库 employee 表中

Sql代码   收藏代码
  1. CREATE OR REPALCE TRIGGER minimun_age_check  
  2. BEFORE INSERT ON employee  
  3. FOR EACH ROW  
  4. BEGIN  
  5.       IF ADD_MONTHS( :new.birth_date, 18*12) > SYSDATE  
  6.       THEN  
  7.          RAISE_APPLICATION_ERROR(-20001, 'Employees must at least eighteen years of age.');  
  8.       END IF;  
  9. END;  
CREATE OR REPALCE TRIGGER minimun_age_check
BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
      IF ADD_MONTHS( :new.birth_date, 18*12) > SYSDATE
      THEN
         RAISE_APPLICATION_ERROR(-20001, 'Employees must at least eighteen years of age.');
      END IF;
END;



在客户端,你可以写一个类似下面的程序,来测试一下。

Sql代码   收藏代码
  1. DECLARE  
  2.    
  3.     no_babies_allowed EXCEPTION;  
  4.    
  5.      /*将名称与用于触发器中的错误号码关联起来*/  
  6.      PRAGMA EXCEPTION_INIT(no_babies_allowed, -20001);  
  7.    
  8. BEGIN  
  9.    
  10.        INSERT INTO employee ....;  
  11.    
  12. EXCEPTION  
  13.        WHEN no_babies_allowed  
  14.        THEN  
  15.               /*  
  16.               || SQLERRM 将传递给内置过程 RAISE_APPLICATION_ERROR 的消息返回  
  17.               */  
  18.               DBMS_OUTPUT.PUT_LINE(SQLERRM);  
  19. END;  
DECLARE
 
    no_babies_allowed EXCEPTION;
 
     /*将名称与用于触发器中的错误号码关联起来*/
     PRAGMA EXCEPTION_INIT(no_babies_allowed, -20001);
 
BEGIN
 
       INSERT INTO employee ....;
 
EXCEPTION
       WHEN no_babies_allowed
       THEN
              /*
              || SQLERRM 将传递给内置过程 RAISE_APPLICATION_ERROR 的消息返回
              */
              DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;


参考资料:
http://oracle.chinaitlab.com/install/355784.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值