|
【赛迪网-it技术报道】oracle developer以其快速的数据处理开发而闻名,其异常处理机制也是比较完善,不可小觑。
1、 异常的优点
如果没有异常,在程序中,应当检查每个命令的成功还是失败,如
begin
select ...
-- check for ’no data found’ error
select ...
-- check for ’no data found’ error
select ...
-- check for ’no data found’ error
这种实现的方法缺点在于错误处理没有与正常处理分开,可读性差,使用异常,可以方便处理错误,而且异常处理程序与正常的事务逻辑分开,提高了可读性,如
begin
select ...
select ...
select ...
...
exception
when no_data_found then -- catches all ’no data found’ errors
2、 异常的分类
有两种类型的异常,一种为内部异常,一种为用户自定义异常,内部异常是执行期间返回到pl/sql块的oracle错误或由pl/sql代码的某操作引起的错误,如除数为零或内存溢出的情况。用户自定义异常由开发者显示定义,在pl/sql块中传递信息以控制对于应用的错误处理。
每当pl/sql违背了oracle原则或超越了系统依赖的原则就会隐式的产生内部异常。因为每个oracle错误都有一个号码并且在pl/sql中异常通过名字处理,oracle提供了预定义的内部异常。如select into 语句不返回行时产生的oracle异常no_data_found。对于预定义异常,现将最常用的异常列举如下:
exception oracle error sqlcode value condition
no_data_found ora-01403 +100 select into 语句没有符合条件的记录返回
too_mang_rows ora-01422 -1422 select into 语句符合条件的记录有多条返回
dup_val_on_index ora-00001 -1 对于数据库表中的某一列,该列已经被限制为唯一索引,程序试图存储两个重复的值
value_error ora-06502 -6502 在转换字符类型,截取或长度受限时,会发生该异常,如一个字符分配给一个变量,而该变量声明的长度比该字符短,就会引发该异常
storage_error ora-06500 -6500 内存溢出
zero_divide ora-01476 -1476 除数为零
case_not_found ora-06592 -6530 对于选择case语句,没有与之相匹配的条件,同时,也没有else语句捕获其他的条件
cursor_already_open ora-06511 -6511 程序试图打开一个已经打开的游标
timeout_on_resource ora-00051 -51 系统在等待某一资源,时间超时
如果要处理未命名的内部异常,必须使用others异常处理器或pragma exception_init 。pragma由编译器控制,或者是对于编译器的注释。pragma在编译时处理,而不是在运行时处理。exception_init告诉编译器将异常名与oracle错误码结合起来,这样可以通过名字引用任意的内部异常,并且可以通过名字为异常编写一适当的异常处理器。
在子程序中使用exception_init的语法如下:
pragma exception_init(exception_name, -oracle_error_number);
在该语法中,异常名是声明的异常,下例是其用法:
declare
deadlock_detected exception;
pragma exception_init(deadlock_detected, -60);
begin
... -- some operation that causes an ora-00060 error
exception
when deadlock_detected then
-- handle the error
end;
对于用户自定义异常,只能在pl/sql块中的声明部分声明异常,异常的名字由exception关键字引入:
reserved_loaned exception
产生异常后,控制传给了子程序的异常部分,将异常转向各自异常控制块,必须在代码中使用如下的结构处理错误:
exception
when exception1 then
sequence of statements;
when exception2 then
sequence of statements;
when others then
3、异常的抛出
由三种方式抛出异常
◆1. 通过pl/sql运行时引擎
◆2. 使用raise语句
◆3. 调用raise_application_error存储过程
当数据库或pl/sql在运行时发生错误时,一个异常被pl/sql运行时引擎自动抛出。异常也可以通过raise语句抛出
raise exception_name;
显式抛出异常是程序员处理声明的异常的习惯用法,但raise不限于声明了的异常,它可以抛出任何任何异常。例如,你希望用timeout_on_resource错误检测新的运行时异常处理器,你只需简单的在程序中使用下面的语句:
raise timeout_on_resouce;
比如下面一个订单输入的例子,若当订单小于库存数量,则抛出异常,并且捕获该异常,处理异常
declare
inventory_too_low exception;
---其他声明语句
begin
if order_rec.qty>inventory_rec.qty then
raise inventory_too_low;
end if
exception
when inventory_too_low then
order_rec.staus:='backordered';
end;
raise_application_error内建函数用于抛出一个异常并给异常赋予一个错误号以及错误信息。自定义异常的缺省错误号是+1,缺省信息是user_defined_exception。raise_application_error函数能够在pl/sql程序块的执行部分和异常部分调用,显式抛出带特殊错误号的命名异常。 raise_application_error(error_number,message[,true,false]))
错误号的范围是-20,000到-20,999。错误信息是文本字符串,最多为2048字节。true和false表示是添加(true)进错误堆(error stack)还是覆盖(overwrite)错误堆(false)。缺省情况下是false。
如下代码所示:
if product_not_found then
raise_application_error(-20123,'invald product code' true);
end if;
4、异常的处理
pl/sql程序块的异常部分包含了程序处理错误的代码,当异常被抛出时,一个异常陷阱就自动发生,程序控制离开执行部分转入异常部分,一旦程序进入异常部分就不能再回到同一块的执行部分。下面是异常部分的一般语法:
exception
when exception_name then
code for handing exception_name
[when another_exception then
code for handing another_exception]
[when others then
code for handing any other exception.]
用户必须在独立的when子串中为每个异常设计异常处理代码,when others子串必须放置在最后面作为缺省处理器处理没有显式处理的异常。当异常发生时,控制转到异常部分,oracle查找当前异常相应的when..then语句,捕捉异常,then之后的代码被执行,如果错误陷阱代码只是退出相应的嵌套块,那么程序将继续执行内部块end后面的语句。如果没有找到相应的异常陷阱,那么将执行when others。在异常部分when 子串没有数量限制。
exception
when inventory_too_low then
order_rec.staus:='backordered';
replenish_inventory(inventory_nbr=>
inventory_rec.sku,min_amount=>order_rec.qty-inventory_rec.qty);
when discontinued_item then
--code for discontinued_item processing
when zero_divide then
--code for zero_divide
when others then
--code for any other exception
end;
当异常抛出后,控制无条件转到异常部分,这就意味着控制不能回到异常发生的位置,当异常被处理和解决后,控制返回到上一层执行部分的下一条语句。
begin
declare
bad_credit exception;
begin
raise bad_credit;
--发生异常,控制转向;
exception
when bad_credit then
dbms_output.put_line('bad_credit');
end;
--bad_credit异常处理后,控制转到这里
exception
when others then
--控制不会从bad_credit异常转到这里
--因为bad_credit已被处理
end;
当异常发生时,在块的内部没有该异常处理器时,控制将转到或传播到上一层块的异常处理部分。
begin
declare ---内部块开始
bad_credit exception;
begin
raise bad_credit;
--发生异常,控制转向;
exception
when zero_divide then --不能处理bad_credite异常
dbms_output.put_line('divide by zero error');
end --结束内部块
--控制不能到达这里,因为异常没有解决;
--异常部分
exception
when others then
--由于bad_credit没有解决,控制将转到这里
end;
5、异常的传播
没有处理的异常将沿检测异常调用程序传播到外面,当异常被处理并解决或到达程序最外层传播停止。在声明部分抛出的异常将控制转到上一层的异常部分。
begin
executable statements
begin
today date:='syadate'; --errror
begin --内部块开始
dbms_output.put_line('this line will not execute');
exception
when others then
--异常不会在这里处理
end;--内部块结束
exception
when others then
处理异常
end
执行部分抛出的异常将首先传递到同一块的异常部分,如果在同一块的异常部分没有处理这个异常的处理器,那么异常将会传播到上一层的异常部分中,一直到最外层。
在异常部分抛出的异常将控制转到上一层的异常部分。
另外错误报告函数sqlcode和sqlerrm在others处理器中特别有用,因为它们返回oracle错误代码和消息。如下例所示:
declare
err_num number;
err_msg varchar2(100);
begin
...
exception
when others then
err_num := sqlcode;
err_msg := substr(sqlerrm, 1, 100);
insert into errors values (err_num, err_msg);
end;
2007-5-7 19:09:02 查看评语»»»
2007-5-7 19:09:43 oracle 异常处理
~~~~有关于异常处理的3个知识点!
1.exception_int编译指示
功能是将某命名异常同某特定oracle错误关联起来.
主用用来捕捉某特定异常错误,而不是通过others来处理.
语法:progma exception_init(exception_name,oracle_error_number)
sql>
sql> declare
2 expa exception;
3 pragma exception_init(expa,-6502);
4 vn number(1);
5 begin
6 vn:=24;
7 exception
8 when expa then
9 dbms_output.put_line('数字或值错误 : 数值精度太高');
10 when others then
11 dbms_output.put_line(sqlcode);
12 dbms_output.put_line(substr(sqlerrm,1,100));
13 end;
14 /
数字或值错误 : 数值精度太高
pl/sql procedure successfully completed
sql>
2.raise_application_error
功能用来自定义错误消息
语法: raise_application_error(error_number,error_message,[keep_errors])
error_number:-20 000到-20 999之间的值
error_message:必须少于512个字符
keep_errors:布尔值.为真则新的错误将被加到已存在的错误清单中(如果已存在的话),为假(默认值)则新的错误将代替当前的错误清单.
sql> begin
2 update t set a='test' where a='12345';
3 if sql%notfound then
4 raise_application_error(-20001,'你傻了?知道没有这样的数据还去更新!');
5 end if;
6 exception
7 when others then
8 dbms_output.put_line(substr(sqlerrm,1,100));
9 end;
10 /
ora-20001: 你傻了?知道没有这样的数据还去更新!
pl/sql procedure successfully completed
sql>
3.异常传播
a可执行部分发生的异常
1)如果当前语句块有该异常的处理程序则执行之,控制权交由外层语句块.
当前语句块有该异常的处理程序:
sql> declare
2 expa exception;
3 expb exception;
4 begin
5 begin
6 raise expa;
7 exception
8 when expa then
9 dbms_output.put_line('异常a产生');
10 end;
11 exception
12 when expb then
13 dbms_output.put_line('异常b产生');
14 end;
15 /
异常a产生
pl/sql procedure successfully completed
sql>
2)如果当前语句没有该异常的处理程序则通过外层语句块中产生该异常来传播该异常,然后通过外层异常处理程序按步骤1来处理.若外层没有该异常的处理程序则异常传播到调用环境.
当前语句块没有该异常的处理程序,外层语句块中产生该异常并处理
sql> declare
2 expa exception;
3 expb exception;
4 begin
5 begin
6 raise expb;
7 exception
8 when expa then
9 dbms_output.put_line('异常a产生');
10 end;
11 exception
12 when expb then
13 dbms_output.put_line('异常b产生');
14 end;
15 /
异常b产生
pl/sql procedure successfully completed
sql>
当前语句块没有该异常的处理程序,外层语句块中产生该异常且没有该异常处理程序,异常传播到调用环境
sql> declare
2 expa exception;
3 expb exception;
4 begin
5 begin
6 raise expb;
7 exception
8 when expa then
9 dbms_output.put_line('异常a产生');
10 end;
11 exception
12 when expa then
13 dbms_output.put_line('异常a产生');
14 end;
15 /
declare
expa exception;
expb exception;
begin
begin
raise expb;
exception
when expa then
dbms_output.put_line('异常a产生');
end;
exception
when expa then
dbms_output.put_line('异常a产生');
end;
ora-06510: pl/sql: 无法处理的用户自定义异常事件
ora-06512: 在line 6
sql>
b声明部分发生的异常
声明部分某赋值发生异常,该异常被立即传播到外层语句块,之后按"a可执行部分发生的异常"规则来处理
sql> begin
2 declare
3 vn number(1):=25;
4 begin
5 null;
6 exception
7 when others then
8 dbms_output.put_line('异常在内层被处理!');
9 end;
10 exception
11 when others then
12 dbms_output.put_line('异常在外层被处理!');
13 end;
14 /
异常在外层被处理!
pl/sql procedure successfully completed
sql>
c异常部分发生的异常
异常处理器中也会产生异常,如raise或运行错误产生,这里异常立即被传播到外层同"b声明部分发生的异常"
例子1
sql>
sql> begin
2 declare
3 expa exception;
4 expb exception;
5 begin
6 raise expa;
7 exception
8 when expa then
9 raise expb;
10 when expb then
11 --这里虽然有expb异常处理语句,但是该异常会立即传播到外层
12 dbms_output.put_line('内层捕捉到异常b');
13 end;
14 exception
15 when others then
16 dbms_output.put_line('外层捕捉到异常b');
17 end;
18 /
外层捕捉到异常b
pl/sql procedure successfully completed
sql>
例子2
sql> desc t;
name type
---- ---------
id number(1)
sql> begin
2 declare
3 expa exception;
4 begin
5 raise expa;
6 exception
7 when expa then
8 insert into t values(12);
9 when others then
10 dbms_output.put_line('内层捕捉到异常');
11 end;
12 exception
13 when others then
14 dbms_output.put_line('外层捕捉到异常');
15 end;
16 /
外层捕捉到异常
pl/sql procedure successfully completed
sql>
2007-5-7 19:10:25 一、在plsql中,oracle自带的exceptions如下:
exception oracle error sqlcode value
access_into_null ora-06530 -6530
case_not_found ora-06592 -6592
collection_is_null ora-06531 -6531
cursor_already_open ora-06511 -6511
dup_val_on_index ora-00001 -1
invalid_cursor ora-01001 -1001
invalid_number ora-01722 -1722
login_denied ora-01017 -1017
no_data_found ora-01403 +100
not_logged_on ora-01012 -1012
program_error ora-06501 -6501
rowtype_mismatch ora-06504 -6504
self_is_null ora-30625 -30625
storage_error ora-06500 -6500
subscript_beyond_count ora-06533 -6533
subscript_outside_limit ora-06532 -6532
sys_invalid_rowid ora-01410 -1410
timeout_on_resource ora-00051 -51
too_many_rows ora-01422 -1422
value_error ora-06502 -6502
zero_divide ora-01476 -1476
2007-5-7 21:45:42 oracle sqlcode/sqlerrm
oracle内置函数sqlcode和sqlerrm是特别用在others处理器中,分别用来返回oracle的错误代码和错误消息。
others处理器应该是异常处理块中的最后的异常处理器,因为它是用来捕获除了别的异常处理器处理以外的所有的oracle异常,所以在程序的最外层使用一个others处理器的话,将可以确保所有的错误都会被检测到。
在一个内在的异常中,sqlcode返回oracle错误的序号,而sqlerrm返回的是相应的错误消息,错误消息首先显示的是错误代码。sqlcode返回的是负数,除非oracle的错误为“ora-01403:no data found”(译:ora-01403:未找到数据),当oracle错误为“ora-01403:no data found”时,其对应的sqlcode为+100。对于用户自定义的异常,sqlcode返回的是+1,而sqlerrm返回的是user-defined exception。
一个oracle的错误消息最多只能包含512个字节的错误代码。如果没有异常被触发,则sqlcode返回0,sqlerrm返回“ora-0000:normal, successful completion”。
|