--异常处理
--规则:
1.exception关键字
2.PL/SQL异常没有任何父子关系
3.when others为最后一条子句,捕获所有未处理的异常
4.可设置多个异常代理代码
5.异常快一次运行,只有一个异常处理器处理异常
--种类:
1.预定义异常--有异常名,错误代码,异常信息,24种,如ORA-0001
2.非预定义异常--无异常名,错误代码,异常信息
3.自定义异常--少用
--举例
declare
r_tb_clazz tb_clazz %rowtype;
begin
select *
into r_tb_clazz
from tb_clazz
where id=1;
exception
when no_data_fount then
dbms_output.putline('数据未找到');--若id=1未找到数据
when others then
dbms_output.putline('others');
end;
--两个异常相关的函数:
--sqlcode:错误代码
--sqlerrm:错误信息
用法:--把异常信息存到表中
1.创建异常表
create table tb_error(
id number primary key,
tablename varchar2(18),
sqlcode varchar2(50),
sqlerrm varchar2(200),
currdate date default sysdate
)
2.创建sequence
create sequence seq_tb_error;
3.--使用
--举例
declare
r_tb_clazz tb_clazz %rowtype;
v_sqlcode varchar2(50),
v_sqlerrm varchar2(200),
begin
select *
into r_tb_clazz
from tb_clazz
where id=1;
exception
when no_data_fount then
v_sqlcode:=sqlcode;
v_sqlerrm:=sqlerrm;
insert into tb_error(id,table,sqlcode,sqlerrm)
values(seq_tb_error,'tablename',v_sqlcode,v_sqlerrm);
when others then
dbms_output.putline('others');
end;
--规则:
1.exception关键字
2.PL/SQL异常没有任何父子关系
3.when others为最后一条子句,捕获所有未处理的异常
4.可设置多个异常代理代码
5.异常快一次运行,只有一个异常处理器处理异常
--种类:
1.预定义异常--有异常名,错误代码,异常信息,24种,如ORA-0001
2.非预定义异常--无异常名,错误代码,异常信息
3.自定义异常--少用
--举例
declare
r_tb_clazz tb_clazz %rowtype;
begin
select *
into r_tb_clazz
from tb_clazz
where id=1;
exception
when no_data_fount then
dbms_output.putline('数据未找到');--若id=1未找到数据
when others then
dbms_output.putline('others');
end;
--两个异常相关的函数:
--sqlcode:错误代码
--sqlerrm:错误信息
用法:--把异常信息存到表中
1.创建异常表
create table tb_error(
id number primary key,
tablename varchar2(18),
sqlcode varchar2(50),
sqlerrm varchar2(200),
currdate date default sysdate
)
2.创建sequence
create sequence seq_tb_error;
3.--使用
--举例
declare
r_tb_clazz tb_clazz %rowtype;
v_sqlcode varchar2(50),
v_sqlerrm varchar2(200),
begin
select *
into r_tb_clazz
from tb_clazz
where id=1;
exception
when no_data_fount then
v_sqlcode:=sqlcode;
v_sqlerrm:=sqlerrm;
insert into tb_error(id,table,sqlcode,sqlerrm)
values(seq_tb_error,'tablename',v_sqlcode,v_sqlerrm);
when others then
dbms_output.putline('others');
end;