1、Oracle系统异常分别为两大类:
系统异常:系统自身提供.
自定义异常:程序执行过程中,出现编程人员认为的非正常情况。对这种异常情况的处理,需要用户在程序中定义,然后显式地在程序中将其引发。
2、 异常案例(no_data_found)
declare
t_name teachers.name%type;
begin
select name into t_name from teachers where teacher_id=1234;
--捕获异常
exception
--如果找不到则提示没有找到数据
when no_data_found then dbms_output.put_line('没有找到员工数据');
--其他异常
when others then dbms_output.put_line('其他异常');
end;
/
3、自定义异常案例
eclare
--定义游标
cursor ts is select name from teachers where teacher_id=1234;
t_name teachers.teacher_id%type;
--自定义例外
no_theacher_name_found exception;
begin
--打开游标
open ts;
--使用fetch取出数据
loop
fetch ts into t_name;
if ts%notfound
then
raise no_theacher_name_found;
end if;
end loop;
--关闭游标
close ts;
--捕获异常
exception
when no_theacher_name_found
then dbms_output.put_line('没有找到该员工数据');
when others then dbms_output.put_line('其他例外');
end;
4、其他异常的定义和使用和定义类似,常见的系统异常大概有24种如下图。
错误号 | 异常错误信息名称 | 说明 |
ORA-0001 | Dup_val_on_index | 违反了唯一性限制 |
ORA-0051 | Timeout-on-resource | 在等待资源时发生超时 |
ORA-0061 | Transaction-backed-out | 由于发生死锁事务被撤消 |
ORA-1001 | Invalid-CURSOR | 试图使用一个无效的游标 |
ORA-1012 | Not-logged-on | 没有连接到ORACLE |
ORA-1017 | Login-denied | 无效的用户名/口令 |
ORA-1403 | No_data_found | SELECT INTO没有找到数据 |
ORA-1422 | Too_many_rows | SELECT INTO 返回多行 |
ORA-1476 | Zero-divide | 试图被零除 |
ORA-1722 | Invalid-NUMBER | 转换一个数字失败 |
ORA-6500 | Storage-error | 内存不够引发的内部错误 |
ORA-6501 | Program-error | 内部错误 |
ORA-6502 | Value-error | 转换或截断错误 |
ORA-6504 | Rowtype-mismatch | 宿主游标变量与 PL/SQL变量有不兼容行类型 |
ORA-6511 | CURSOR-already-OPEN | 试图打开一个已处于打开状态的游标 |
ORA-6530 | Access-INTO-null | 试图为null 对象的属性赋值 |
ORA-6531 | Collection-is-null | 试图将Exists 以外的集合( collection)方法应用于一个null pl/sql 表上或varray上 |
ORA-6532 | Subscript-outside-limit | 对嵌套或varray索引得引用超出声明范围以外 |
ORA-6533 | Subscript-beyond-count | 对嵌套或varray 索引得引用大于集合中元素的个数. |