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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM