Oracle异常的抛出处理

--一异常处理的代码
--sqlcode 异常编号
--sqlerrm 信号字符串

/*

在plsql 块中格式

Declare
  变量
Begin
    代码块
    
    EXCEPTION
        when 异常的名称  then
            如生上面的异常时做的具体工作。
End;

*/

set serveroutput on;
create or replace procedure pr12
as
--定义一个int变liang
v_age integer;
v_name varchar(30);
begin 
v_age:=89;
--通过select给v_name设置值
--修改成过程
select name into v_name from stud where id=1;
DBMS_OUTPUT.PUT_LINE('没有出错');
exception
 when value_error then 
 SYS.DBMS_OUTPUT.PUT_LINE('数值错误');
 when no_data_found then 
 SYS.DBMS_OUTPUT.PUT_LINE('没有数据');
 when others then
 SYS.DBMS_OUTPUT.PUT_LINE(sqlcode||'你出错了'||sqlerrm);
 end;

exec pr12();
-----------------------------------------
--自定义异常自己抛出异常/
/*
定义一个自己的异常
      myException Exception;
抛出异常
    RAISE myException;
    
    处理自己的异常:
        Exception 
            When myException then
                ....
*/
set serveroutput on;
declare
myEx exception;
begin
DBMS_OUTPUT.PUT_LINE('这里没错');
raise myEx;
DBMS_OUTPUT.PUT_LINE('不会输出,前面抛出异常');
--处理异常
exception
when myEx then
DBMS_OUTPUT.PUT_LINE('自己的异常'||sqlcode||'  '||sqlerrm);
when others then 
DBMS_OUTPUT.PUT_LINE('不知知道什么错误'||sqlcode||sqlerrm);
END;
---出错直接抛出

declare
begin
DBMS_OUTPUT.PUT_LINE('no errors');
--直接抛出
RAISE_APPLICATION_ERROR(-20000, 'A');
DBMS_OUTPUT.PUT_LINE('go okk....');
exception
   when others then
DBMS_OUTPUT.PUT_LINE(sqlcode||'  '||sqlerrm);
end;

转载于:https://www.cnblogs.com/xiaweifeng/p/3677058.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值