----------------------------oracle异常处理实例
declare
n number( 9);
begin
n:=&n;
case
when n>200 then
dbms_output.put_line( '>200');
when n>10 then
dbms_output.put_line( '>10');
end case;
----异常处理部分
exception
when CASE_NOT_FOUND then ----CASE_NOT_FOUND 当在case ..when.字句中没有匹配的项目,而有没有else 结尾时 就会抛出这个异常
dbms_output.put_line( 'inid');
end;
错误号 | 异常错误信息名称 | 说明 |
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 索引得引用大于集合中元素的个数. |
---------------自定义异常---------------
declare
emp_dateNotFound_exception exception ; -------定义异常
begin
update emp set sal=sal+ 100 where empno=&empno;
if sql % notfound then
raise emp_dateNotFound_exception; -----抛出异常
else
dbms_output.put_line( 'ok' );
end if ;
exception
when emp_dateNotFound_exception then ------捕获异常
dbms_output.put_line( '自定义异常被捕获了' );
dbms_output.put_line('code: '||sqlcode); ---获得错误的异常的编号
dbms_output.put_line( 'errm: ' ||sqlerrm ); ---获得异常的信息
when others then
dbms_output.put_line('其他的错误');
dbms_output.put_line('code: '||sqlcode); ---获得错误的异常的编号
dbms_output.put_line( 'errm: ' ||sqlerrm ); ---获得异常的信息
end;