异常 exception

【功能】
针对已经编绎通过的块/存储过程,在运行过程中,也可能会报错,异常处理可以将报错的信息进行记录并返回,另外,也可以自定义异常。

【格式】
declare
自定义异常变量 exception;
begin
–数据处理
exception
when 自定义变量 then
–自定义异常处理
when others then
–系统异常处理
end;

【注意事项】
1.异常有两种:自定义异常(需要定义异常类型的变量)、系统异常(others)

/自定义异常/
【示例1】
需求描述:
create or replace procedure <用户名>.<存储过程名>
(
iv_date in varchar2, --统计日期
oi_return out integer --结束成功否标志
)
is
vv_task_pos varchar2(50); --任务位置
vi_err_code integer; --出错代码
vv_err_msg varchar2(200); --出错信息
exc_return exception; --自义定异常
begin

vv_task_pos := ‘检查输入参数’;
if (iv_date is null) then
vi_err_code := -1;
vv_err_msg := ‘没有输入统计日期参数’;
raise exc_return;
end if;

exception
when exc_return then
rollback;
oi_return := vi_err_code;
end;

/系统异常/

【示例2】
begin
null;
exception
when others then
vi_err_code := sqlcode;
vv_err_msg := substr(sqlerrm, 1, 200);
rollback;
oi_return := vi_err_code;
end;


示例3:

declare
t varchar2(10);
a varchar2(100);
c varchar2(100);
e exception;
begin
select serv_name into t from tb_sver_name where serv_num=‘13116371042’;
dbms_output.put_line(t);
select serv_name into t from tb_sver_name where 1=2;
dbms_output.put_line(t);
select serv_name into t from tb_sver_name where serv_num=‘13116371043’ ;
if t=‘gwei’ then
raise e;
end if ;

exception
when e then
delete from tb_sver_name where serv_name=‘gwei’;
dbms_output.put_line(‘我来过’);
–commit;
when too_many_rows then --系统异常:SELECT INTO 返回多行时报的错会跳到该异常
dbms_output.put_line(‘返回多行’);
when no_data_found then --系统异常:SELECT INTO 无结果集时会跳到该异常
dbms_output.put_line(‘无结果集’);
when others then --others:全部异常错误都会接收到此,一般与sqlcode、sqlerrm配合使用
dbms_output.put_line(sqlcode||sqlerrm);
end;

–系统异常:不需要exception上面做任何处理,只等着异常发生并跳到exception中的某个异常中
–自定义异常:需要在begin与exception之间设计异常 1.变量区(声明异常 exception类型) 2.关键字raise(begin后 ) 3.exception when e(异常名) then

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值