`

Oracle 10g处理例外(即sql异常)学习一——预定义例外,即常见例外

阅读更多
Oracle提供了预定义例外、非预定义例外和自定义例外等三种例外类型,其中预定义例外用于常见的Oracle错误;非预定义例外则用于预定义例外所不能处理的错误;自定义例外用于处理与Oracle错误无关的其他情况
1、例外的格式
declare
v_name communitytype.name%type;
begin
select name into v_name
from communitytype
where community_type_id = 'ebook';
dbms_output.put_line(v_name);
--捕捉异常
exception
when too_many_rows then 
dbms_output.put_line('查询只能返回单行');
when no_data_found then
dbms_output.put_line('没有找到任何记录');
when others then
dbms_output.put_line('查询出现异常');
end;


2、处理预定义列外
预定义例外是指有pl/sql所提供的系统例外。当pl/sql应用程序违反了Oracle规则或者系统限制时,则会隐含的触发一个内部例外。pl/sql为开发人员提供了大概20多个例外,每个例外都对应一个Oracle系统错误。
1)常用预定义例外

access_into_null 对应于ORA-06530,没有初始化对象就直接给对象赋值

case_not_found 对应于ORA-06592,当在pl/sql块中编写case语句时,如果在when子句中没有包含必须的条件分支,并前没有包含else子句,就会隐含地厨房case_not_fount例外
declare
v_name communitytype.name%type;
begin
select name into v_name 
from communitytype t 
where t.community_type_id = 'ebook';
case
when v_name = '123' then
dbms_output.put_line('name为123');
end case;
exception
when case_not_found then
dbms_output.put_line('没有将所有情况考虑完全');
end;


    没有将所有情况考虑完全


collection_is_null 对应于ORA-06531,在给集合元素赋值前,必须初始化集合元素。如果没有初始化集合元素,则会隐含地触发collection_is_null
declare
--集合类型(索引表)
type ename_table_type is table of communitytype.name%type;
ename_table ename_table_type;
begin
select name into ename_table(1)
from communitytype
where community_type_id = 'ebook';
dbms_output.put_line(ename_table(1));
exception
when collection_is_null then
dbms_output.put_line('集合类型没有初始化');
end;


    集合类型没有初始化


cursor_already_open 对应于OR-06511,当重新打开已经打开的游标时,会触发该例外。如果用户已经使用open命令打开了现实游标,并执行游标for循环时,就会隐含地触犯该例外,因为游标for循环会隐含地打开游标。
declare
cursor emp_cursor is
select name from communitytype;
begin
open emp_cursor;
for emp_record in emp_cursor loop
dbms_output.put_line(emp_record.name);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('游标已经打开');
end;


    游标已经打开


dup_val_on_index 对应于ORA-001,当在唯一索引所对应的列上插入重复值,就会隐含地触发该例外,比较常见的情况就是主键冲突,也就是插入重复的主键。

invalid_cursor 对应于ORA-01001,当试图在不合法的游标上执行操作时,就会隐含地触发该例外,例如,如果要从未打开的游标中提取数据,或者关闭未打开的游标时,就会触发该例外。
declare
cursor emp_cursor is
select name from communitytype;
emp_record emp_cursor%rowtype;
begin
fetch emp_cursor into emp_record;
close emp_cursor;
exception
when invalid_cursor then
dbms_output.put_line('关闭还没有打开的游标,抛出异常');
end;


    关闭还没有打开的游标,抛出异常


invalid_number 对应于ORA-01722,当内嵌的sql语句不能将有效的字符转换为数字时,会隐含地触发该例外。
declare
v_number int;
begin
select 1+'1OO' into v_number from dual;
exception
when invalid_number then
dbms_output.put_line('不能有效地将字符转换为数字');
end;


    不能有效地将字符转换为数字


no_data_found 对应于ORA-01403,当执行select into时未返回行,或者引用了索引表未初始化的元素时,会触发该例外

too_many_rows 对应于ORA-01422,当查询单行记录时,返回多行记录就会触发该例外。

zero_divide 对应于ORA-01476,如果用数字值除0时,会隐含触发该例外。

subscript_beyond_count 对应于ORA-06533,当使用嵌套表或变长数组varray元素时,下标超出范围时,则会隐含地触发该例外。
declare
type emp_array_type is varray(20) of varchar2(10);
emp_array emp_array_type;
begin
emp_array:=emp_array_type('郑田','胡杰');
--变长数组下标从1开始
dbms_output.put_line(emp_array(3));
exception
when subscript_beyond_count then
dbms_output.put_line('数组越界,超出下标');
end;


    数组越界,超出下标


subscript_outside_limit 对应于ORA-06532,当使用嵌套表和变长数组时,如果元素下表为负或者为0时,会触发该例外
declare
type emp_array_type is varray(20) of varchar2(10);
emp_array emp_array_type;
begin
emp_array:=emp_array_type('郑田','胡杰');
--变长数组下标从1开始
dbms_output.put_line(emp_array(0));
exception
when subscript_outside_limit then
dbms_output.put_line('数据表和变长数组下标不能为负');
end;


    数据表和变长数组下标不能为负


value_error 对应于ORA-06502,当在执行赋值操作时,如果变量长度不足以容纳实际数据时,则会隐含地触发该例外.
declare
v_ename varchar2(5);
begin
select name into v_ename
from communitytype
where community_type_id = 'ebook';
exception
when value_error then
dbms_output.put_line('变量尺寸不足');
end;


    变量尺寸不足



其他预定义例外
1、login_denied 对应于ora-01017错误,当用户链接oracle数据库时,如果提供了不正确的用户名或口令,则会触发该例外

2、not_logged_on 对应于ora-01012错误,如果应用程序没有链接到oracle数据库,那么在执行pl/sql块中访问数据库时,会触发该例外。

3、program_error 对应于ora-06501错误,表示存在pl/sql内部问题,用户此时可能要重新安装数据字典和pl/sql系统包

4、rowtype mismatch 对应于ora-06504错误,当执行赋值操作时,如果宿主游标变量和pl/sql游标变量的返回类型不兼容,会触发该例外。

5、self_is_null 对应于ora-30625错误,当使用对象类型时,如果在null实例上调用成员方法,会触发该实例。

6、storage_error 对应于ora-06500错误,当pl/sql块运行时,如果超出内存空间或者内存被损坏,会触发该例外。

7、sys_invalid_rowid 对应于ora-01410错误,当将字符串转变为rowid时,必须使用有效的字符串,如果使用的无效的字符串,会触发该例外。

8、timeout_on_resource 对应于ora-00051错误,如果oracle在等待资源时,出现超时错误,会触发该例外
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics