pl/sql异常处理初步

来源:百度文库 编辑:神马文学网 时间:2024/06/06 04:20:07
pl/sql处理异常不同于其他程序语言的错误管理方法,pl/sql的异常处理机制与ada很相似,有一个处理错误的全包含方法。当发生错误时,程序无条件转到异常处理部分,这就要求代码要非常干净并把错误处理部分和程序的其它部分分开。oracle允许声明其他异常条件类型以扩展错误/异常处理。这种扩展使pl/sql的异常处理非常灵活。
当一个运行时错误发生时,称为一个异常被抛出。pl/sql程序编译时的错误不是能被处理得异常,只有在运行时的异常能被处理。在pl/sql程序设计中异常的抛出和处理是非常重要的内容。
抛出异常
由三种方式抛出异常
. 通过pl/sql运行时引擎
. 使用raise语句
. 调用raise_application_error存储过程
当数据库或pl/sql在运行时发生错误时,一个异常被pl/sql运行时引擎自动抛出。异常也可以通过raise语句抛出
raise exception_name;
显式抛出异常是程序员处理声明的异常的习惯用法,但raise不限于声明了的异常,它可以抛出任何任何异常。例如,你希望用timeout_on_resource错误检测新的运行时异常处理器,你只需简单的在程序中使用下面的语句:
raise timeout_on_resouce;
下面看一个订单输入系统,当库存小于订单时抛出一个inventory_too_low异常。
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‘‘;
replenish_inventory(inventory_nbr=>
inventory_rec.sku,min_amount=>order_rec.qty-inventory_rec.qty);
end;
这里replenish_inventory是一个触发器。
处理异常
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;
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;
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;
异常传播
没有处理的异常将沿检测异常调用程序传播到外面,当异常被处理并解决或到达程序最外层传播停止。
在声明部分抛出的异常将控制转到上一层的异常部分。
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
执行部分抛出的异常将首先传递到同一块的异常部分,如果在同一块的异常部分没有处理这个异常的处理器,那么异常将会传播到上一层的异常部分中,一直到最外层。
在异常部分抛出的异常将控制转到上一层的异常部分。
处理异常将停止异常的传播和解决。有时用户希望在错误发生时,程序仍然能执行一些动作,要达到这个目的,可以把希望执行的动作放在异常处理器中,然后执行不带参数的raise语句,raise语句将重新抛出出现的异常,允许他继续传播。
declare
order_too_old exception;
begin
raise order_too_old;
exception
when order_too_old then
declare
file_handle utl_file.file_type;
begin
--open file
file_handle:=utl_file.fopen
(location=>‘‘/ora01/app/oracle/admin/test/utlsir‘‘
,filename=>‘‘error.log‘‘
.open_mode=>‘‘w‘‘);
--write error stack
utl_file.put_line(filehandle,
dbms_utility.format_error_stack);
--write the call stack
utl_file.put_line(filehandle,
dbms_utility.format_call_stack);
--close error log
utl_file.fclose(file_handle);
raise; --re-raise the exception
end
end
如果从format_xxx_stack输出一个很大的值,那么使用dbms_output或utl_file显示错误或调用堆的异常部分自身也会抛出异常,这两个堆常规下最多能返回2000字节,但utl_file.put_line被限制在1000字节以内,而dbms_output.put_line限制在512字节内。如果使用前面的代码并且不允许这种可能性,那么在异常处理器中将抛出一个未处理的异常。
goto语句不能用于将控制从执行部分传递到异常部分或反之。
已命名异常
在pl/sql块的异常部分只有已命名的异常才能被when子串处理,oracle包含了一系列已命名的异常,这些异常都声明在standard包中,这些内建异常在这里就不一一讲述,有兴趣的读者可以查阅有关资料。
常用异常处理方法
除了标准异常外,用户可以声明自己的已命名异常。比如商业规则错误,或者将自定义异常与一个数据库号相联系,甚至可以将数据库错误号赋予自定义异常。pl/sql的这个性能极大的提高了对异常的管理和处理的能力。
声明自己的异常
如果你希望声明自己的异常,那么你可以在异常处理器中包括when子句,声明的异常的作用范围与声明的变量相同。声明在外层块的异常能被该块和它的子块访问,但声明在子块的异常不能被父块处理。
例:
begin
declare
insufficient_credite exception;
begin
rasise insufficient_credite;
exception
when insufficient_credite then
--可以在此处理异常
extend_credite(cust_id);
end -嵌套块结束
exception
when insufficient_credite then
--超出范围,不能在这里处理异常
end;
如果声明的异常与内建的异常同名,那么当引用异常将解决你的异常而不是内建的异常。
给数据库错误命名
如果希望处理一个异常,那么异常必须有一个名字,数据库错误有数千个,但是只有不到25个是内建的已命名异常,但需要处理这些未命名的异常时,你可以将一个名字和一个错误号联系在一起,达到这个目的的语句是:pragma exception_init语句
语法如下:
pragma exception_init(exception_name,error_number);
执行这个语句之前,必须首先声明异常名。
declare
invald_table_name exception;
pragma exception_init(invald_table_name,-942);
begin
exception
when invald_table_name then
utl_file.put_line(file_handle,‘‘user‘‘ || uid || ‘‘hit a bad table‘‘);
end;
另外一种处理数据库错误的方法是使用内建函数sqlcode和sqlerrm,这两个函数在包一级声明,sqlcode将返回现行数据库错误号,这些错误号中除了no_data_found是+100外其他都是负数。sqlerrm返回文本描述的错误信息。为了获得用户自定义异常返回的sqlerrm和sqlcode,你需要使用raise_application_error函数给自定义异常标注错误号。
给自定义错误标注号码
raise_application_error内建函数用于抛出一个异常并给异常赋予一个错误号以及错误信息。自定义异常的缺省错误号是+1,缺省信息是user_defined_exception。来自未处理的异常的一般信息对于识别导致错误的原因没有帮助,raise_application_error函数能够在pl/sql程序块的执行部分和异常部分调用,显式抛出带特殊错误号的命名异常。
raise_application_error(error_name,error_message[,{true|| false}]);
错误号的范围是-20,999到-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;