Oracle PL/SQL中异常高级特性

栏目: 数据库 · Oracle · 发布时间: 5年前

内容简介:在OraclePL/SQL语句块中exception的异常处理部分是非常重要的组成部分,它决定了在PL/SQL语句块内部可执行部分在发生异常错误时,程序是友好地提示:程序遇到某些错误而无法执行,还是抛出一堆难以理解的Oracle内部错误码。本文只介绍3中PL/SQL异常的三种高级形态,用于解决Oracle内置异常过少,很多时候不能够满足实际的使用需求。1,RAISE_APPLICATION_ERROR

在OraclePL/SQL语句块中exception的异常处理部分是非常重要的组成部分,它决定了在PL/SQL语句块内部可执行部分在发生异常错误时,程序是友好地提示:程序遇到某些错误而无法执行,还是抛出一堆难以理解的Oracle内部错误码。

本文只介绍3中PL/SQL异常的三种高级形态,用于解决Oracle内置异常过少,很多时候不能够满足实际的使用需求。

1,RAISE_APPLICATION_ERROR

 - 是Oracle提供的一种特殊的内置过程,允许 程序员 为特定的程序创建有意义的错误消息,适用于用户自定义定义异常。

 - 语法结构

  RAISE_APPLICATION_ERROR (error_number,error_message);或者

  RAISE_APPLICATION_ERROR (error_number,error_message,keep_errors)

  - error_number 是与特定错误消息关联的错误编号,Oracle预留了-20999 -- -20000专门提供给程序员自定义错误代码。

  - error_message 是错误消息文本,最多包含2048个字符。

  - keep_errors 是可选的Boolean参数,默认为FALSE,如果为TRUE,新抛出的错误会被添加到已抛出的错误列表中,这个错误列表称为错误栈,如果为FALSE,新错误会替换已抛出的错误栈。

 - 适用于未命名的用户定义异常,负责把错误编号和错误消息关联,用户定义了异常,却没有定义该错误的名称

 - 使用RAISE_APPLICATION_ERROR过程,程序员能够遵循与Oracle一致的方式返回错误消息。

- 示例代码

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(下文有专门的介绍)。

2,EXCEPTION_INIT

 - 使用EXCEPTION_INIT编译指令,可以将用户自定义的Oracle错误编号和用户自定义的错误名称关联起来,相当于用户自定义错误和RAISE_APPLICATION_ERROR的结合体。

 - EXCEPTION_INIT 出现在语句块的声明部分:

  exception_name exception;

  pragma exception_init(exception_name,error_code)

- 考虑如下代码:

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 进行关联。当程序执行报错时进入异常处理部分,在这里我们重新给这个错误定义了错误消息。

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以字面值赋值的方法给这两个变量。

Linux公社的RSS地址: https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-08/153823.htm


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

引爆点

引爆点

[美] 马尔科姆·格拉德威尔 / 钱清、覃爱冬 / 中信出版社 / 2006-1 / 29.80元

这本书是《纽约客》杂志专职作家马尔科姆·格拉德威尔的一部才华横溢之作。他以社会上突如其来的流行风潮研究为切入点,从一个全新的角度探索了控制科学和营销模式。他认为,思想、行为、信息以及产品常常会像传染病爆发一样,迅速传播蔓延。正如一个病人就能引起一场全城流感;如果个别工作人员对顾客大打出手,或几位涂鸦爱好者管不住自己,也能在地铁里掀起一场犯罪浪潮;一位满意而归的顾客还能让新开张的餐馆座无虚席。这些现......一起来看看 《引爆点》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具