例外分为三种:1.预定义例外 2.非预定义例外 2.自定义例外
以下主要举例说明预定义例外处理
1.预定义例外 no_data_found.
declare
v_ename emp ename%type
begin
select ename into v_ename from emp where empno=&gno;
dbms_output.put_line('名字:'||v_ename);
exception
end;
2.预定义例外 case_no_found.
在pl/sql块中编辑case语句时,如果when 子句中没有包含必须的条件分支,就会触发case_no_found.
create or replace procedure xm_pro1(xmno
begin
select sal into v_sal from emp where empno=xmno;
case
when v_sal<5000 then
update emp set sal=sal+1000 where empno=xmno;
when v_sal<8000 then
update emp set sal=sal+2000 where empno=xmno;
end case;
exception
when case_no_found then
dbms_output.put_line('case语句中没有与'||v_sal||'相匹配的条件');
end;
3.预定义例外 dup_val_on_index
在唯一索引所对应的列上插入重复值时,会隐含的触发例外
begin
insert into dept values(10,'财务部','广州');
exception
when dup_val_on_index then
dbms_output.put_line('在deptno 列上不能出现重复值');
end;
4.预定义例外 cursor_already_open.
当重复打开游标时,会触发例外
declare
begin
open emp_cursor;
for emp_record1 in emp_cursor loop;
dbms-output.put_line(emp_record1.ename);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('游标已经打开');
end;
其他预定义例外用法大同小异,就不在多说了。
5. login_denide
6. not_logged_on
7. storage_error
8. timeout_on_resource
...
...
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23410111/viewspace-1115160/ ,如需转载,请注明出处,否则将追究法律责任。