--一異常處理的代碼 --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;