Oracle PLSQL中异常高级特性示例解析


当前第2页 返回上一页

 - 使用EXCEPTION_INIT

declare
 v_no number := &p_no;
 e_dept_exist exception;
 pragma exception_init(e_dept_exist,-02292);
begin
 delete from dept where deptno = v_no;
 dbms_output.put_line(chr(10)||'The department id is '||v_no||' has been deleted');
exception
 when e_dept_exist then
  dbms_output.put_line(chr(10)||'There are some employees in this deptartment, if you want delete this deptartment ,please delete these employees in the department first.');
end;
/ 
Enter value for p_no: 20
old 2: v_no number := &p_no;
new 2: v_no number := 20;
There are some employees in this deptartment, if you want delete this deptartment ,please delete these employees in the department first.
PL/SQL procedure successfully completed.

 - 这下抛出的错误就容易理解多了。首先我们定义了一个名为e_dept_exist的异常,然后将这个异常与Oracle错误代码 -02292 进行关联。当程序执行报错时进入异常处理部分,在这里我们重新给这个错误定义了错误消息。

3,SQLCODE 和 SQLERRM

 - 在异常处理中,当异常的名称未知时(比如上面1中RAISE_APPLICATION_ERROR),都可以使用others来进行异常的捕获处理;

 - 由于others所捕获的异常是未知的(也可以是已知的,但是在程序中没有将其枚举出来),因此需要使用Oracle提供的两个内置函数SQLCODE、SQLERRM来针对others的异常进行处理:

 - SQLCODE 会返回Oracle的错误编号
 - SQLERRM,返回错误的消息

 - 示例1,处理Oracle系统返回的错误:

declare
 v_no number := &p_no;
 error_code number;
 error_msg varchar2(500);
begin
 delete from dept where deptno = v_no;
 dbms_output.put_line(chr(10)||'The department id is '||v_no||' has been deleted');
exception
 when others then
  error_code := sqlcode;
  error_msg := sqlerrm;
  dbms_output.put_line(chr(10)||'Error code is: '||error_code);
  dbms_output.put_line(chr(10)||'Error message is: '||error_msg);
end;
Enter value for p_no: 10
old 2: v_no number := &p_no;
new 2: v_no number := 10;
Error code is: -2292
Error message is: ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found
PL/SQL procedure successfully completed.

 - 请注意exception异常处理部分,在该部分里面我们用到了声明部分定义的两个变量,error_code用来存储SQLCODE,error_msg用来存储SQLERRM。然后将两个变量值打印出来。

 - 示例2,处理用户自定义的异常:

declare
 v_id number := &p_id;
 v_name varchar2(20);
 v_sal number;
begin
 if v_id > 0 then
  select ename,sal into v_name,v_sal from emp where empno = v_id;
  dbms_output.put_line(chr(10)||v_name||' '||v_sal);
 else
  raise_application_error (-20001,'Employee id can not be negative.');
 end if;
exception
 when NO_DATA_FOUND then
  dbms_output.put_line(chr(10)||'There is no such employee id is '||v_id); 
 when others then
  declare
   error_code number;
   error_msg varchar2(500);
  begin
   error_code := sqlcode;
   error_msg := sqlerrm;
   dbms_output.put_line(chr(10)||'Error code is: '||error_code);
   dbms_output.put_line(chr(10)||'Error message is: '||error_msg);
  end;
end;
/
Enter value for p_id: -90
old 2: v_id number := &p_id;
new 2: v_id number := -90;
Error code is: -20001
Error message is: ORA-20001: Employee id can not be negative.
PL/SQL procedure successfully completed.

 - 在本代码中使用了raise_application_error,由于单纯的使用raise_application_error,只能使用others进行捕获。在异常处理部分,我们使用了一个PL/SQL语句块来处理这个错误,声明两个变量,并将SQLCODE和SQLERRM以字面值赋值的方法给这两个变量。

总结

以上所述是小编给大家介绍的Oracle PL/SQL中异常高级特性示例解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...