在OraclePL/SQL语句块中exception的异常处理部分是非常重要的组成部分,它决定了在PL/SQL语句块内部可执行部分在发生异常错误时,程序是友好地提示:程序遇到某些错误而无法执行,还是抛出一堆难以理解的Oracle内部错误码。
本文只介绍3中PL/SQL异常的三种高级形态,用于解决Oracle内置异常过少,很多时候不能够满足实际的使用需求。
1,RAISE_APPLICATION_ERROR
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); end; / Enter value for p_id: 40 old 2: v_id number := &p_id; new 2: v_id number := 40; There is no such employee id is 40 PL/SQL procedure successfully completed. / Enter value for p_id: -90 old 2: v_id number := &p_id; new 2: v_id number := -90; declare * ERROR at line 1: ORA-20001: Employee id can not be negative. ORA-06512: at line 11
- 示例解析:该PL/SQL代码会根据用户输入的员工Id,查询员工的姓名和工资。当我们输入存在的员工编号时,程序能够正常返回结果;如果输入不存在ID,则select into语句会抛出没有返回行,进而使程序进入异常处理部分(本部分为举例),程序同样执行成功;当输入一个负数时,if条件语句就会进入到raise_application_error部分,由于可执行部分运行发生错误,执行焦点会立即转移到异常处理部分,而异常处理部分没有关于该异常的处理,所以程序报错,并返回到用户界面。
- 是哟个raise_application_error,程序员可以使程序实现像Oracle系统产生的错误消息。
- 事实上,单纯使用raise_application_error,因为没有异常的名称,如果要对其进行异常处理,只能够使用others(下文有专门的介绍)。
declare v_no number := &p_no; begin delete from dept where deptno = v_no; dbms_output.put_line(chr(10)||'The department id is '||v_no||' has been deleted'); end; / Enter value for p_no: 20 old 2: v_no number := &p_no; new 2: v_no number := 20; declare * ERROR at line 1: ORA-02292: integrity constraint (SCOTT.FK_DEPTNO) violated - child record found ORA-06512: at line 4
- 由于违反外键约束,删除部门失败了。但是抛出的错误不是很好理解
- 我们可以使用EXCEPTION_INIT来对这个错误进行处理,首先我们得知道违反外键约束的这个Oracle错误代码“ORA-02292”
- 使用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 进行关联。当程序执行报错时进入异常处理部分,在这里我们重新给这个错误定义了错误消息。
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以字面值赋值的方法给这两个变量。