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

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.自定义异常
            create 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 bthen
                    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)