您的位置:首页 > 数据库 > Oracle

oracle中存储过程的三种异常捕获方式

2017-10-23 10:02 302 查看
oracle中存储过程的异常分为:
1.预定义异常:oracle已经定义了一个常量用于表示异常编号
异常          错误编号     常量名称
除数为0       -01476       ZERO_DIVIDE
案例:
create or replace procedure test_
as
c int;
begin
c:=10/0;
exception
when zero_divide then
dbms_output.put_line('除数不能为0哦');
end;

2.非预定义异常:错误编号没有对应的常量
create or replace procedure test_
as
c int;
my_error exception;
pragma exception_init(my_error,-01476);
begin
c:=10/0;
exception
when my_error then
dbms_output.put_line('除数不能为0哦');
when others then
dbms_output.put_line('未知错误');
end;
3.自定义异常
creat
4000
e or replace procedure move_moeny(
u1 int,
u2 int,
money_ dec
)
is
b dec(19,2);  --读取u1余额
my_error exception;  --定义自定义异常
begin
--判断转账的金额是否大于余额
select a.balance into b from account_ a where a.aid=u1;
if b<money_ then
raise my_error;
end if;

--如果余额大于转账金额,u1的余额要减少
update account_ set balance = balance-money_ where aid=u1;
--u2的余额要增加
update account_ set balance =balance+money_ where aid=u2;

--转账成功之后,要添加两条交易记录
insert into transfer values(x.nextval,sysdate,u1,'取出',money_);
insert into transfer values(x.nextval,sysdate,u2,'存入',money_);
exception
when my_error then
dbms_output.put_line('余额不足');
rollback;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle 异常 存储 plsql