--异常的处理
declare
v_serialno varchar2(15);
begin
--制造返回多行的异常
select serialno into v_serialno from temp_dnpath;
dbms_output.put_line('path''s no is ' || v_serialno);
exception
--处理返回多行的异常
when too_many_rows then
dbms_output.put_line('your select statement retrieved multiple rows,consider using a cursor');
--处理其他的异常
when others then
dbms_output.put_line('you meet an error!');
end;
------------------------------异常不能跳回原来的位置
declare
v_serialno varchar2(15);
begin
select serialno into v_serialno from temp_dnpath;
--发生异常后,是不可以跳转会原程序的
<>
dbms_output.put_line('path''s no is ' || v_serialno);
exception
when too_many_rows then
dbms_output.put_line('your select statement retrieved multiple rows,consider using a cursor');
--此处跳转会报错
--goto welcomeback;
--往下跳转还是可以的
goto gohere;
dbms_output.put_line('1,you meet an error!');
<>
dbms_output.put_line('2,you meet an error!');
when others then
dbms_output.put_line('3,you meet an error!');
end;
-------------------------------不再常规异常情况的处理
declare
--申明错误
e_insert_excep exception;
--通知编译器异常的编码
PRAGMA exception_init(e_insert_excep, -1400);
begin
--在不能为空的值插入空值
insert into temp_dnpath(serialno,no) values('abc',null);
exception
when e_insert_excep then
dbms_output.put_line('Insert Operation Failed!');
dbms_output.put_line(SQLERRM);
END;
-------------------------------自定义的异常处理
declare
v_serialno varchar2(30) := 'zhang';
v_no number := 100;
--定义异常
e_invalid_no EXCEPTION;
BEGIN
update temp_dnpath set serialno = v_serialno
where no = v_no;
if sql%notfound then
--抛出
raise e_invalid_no;
end if;
commit;
exception
--处理
when e_invalid_no then
dbms_output.put_line('no such no.');
end;
------------------------------raise_application_error函数增加自定义异常处理的功能
declare
v_serialno varchar2(30) := 'zhang';
v_no number := 100;
--自定义异常
e_invalid_no EXCEPTION;
--通知编译器异常的编码
pragma exception_init(e_invalid_no,-20188);
BEGIN
update temp_dnpath set serialno = v_serialno
where no = v_no;
if sql%notfound then
--raise e_invalid_no;
--输入异常的对应信息
raise_application_error(-20188, 'I write my error message here!');
end if;
commit;
exception
when e_invalid_no then
dbms_output.put_line(SQLCODE || ' ==> ' || SQLERRM);
end;
------------------------------让过程继续执行的二种方法之一,使用begin/end子块
declare
v_temp number(8,2);
begin
--添加begin/end子块
begin
select 399 / 0 into v_temp from dual;
exception
when zero_divide then
dbms_output.put_line(SQLCODE || ' ==> ' || SQLERRM);
end;
--异常处理后跳出,继续后面的处理
dbms_output.put_line('outside');
exception
when zero_divide then
null;
end;
----------------------------让过程继续执行的二种方法之一,用循环多试几次成功后exit
declare
l_serialno temp_dnpath.serialno%type := 'G7600027';
suffix number := 1;
begin
for i in 1..5
loop
begin
--设置会滚,因为要多试几次,保证每次都是干净的
savepoint start_transaction;
delete from temp_dnpath where no = 12;
insert into temp_dnpath values(l_serialno,12);
--成功就提交退出
commit;
exit;
exception
when dup_val_on_index then
--不成功回退
rollback to start_transaction;
suffix := suffix + 1;、
--变化值后,再来。
l_serialno := l_serialno || to_char(suffix);
end;
end loop;
end;
----------------------------使用使用标签,定位异常出现的位置
declare
stmt_no number;
v_serialno temp_dnpath.serialno%type;
begin
--执行select前赋值标识
stmt_no := 1;
select serialno into v_serialno from temp_dnpath where no = 5;
--执行select前赋值标识
stmt_no := 2;
select serialno into v_serialno from temp_dnpath where no = 50;
exception
when no_data_found then
--输出出错位置
dbms_output.put_line('Table name not found in query ' || stmt_no);
end;
end;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15681893/viewspace-711005/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15681893/viewspace-711005/