1. 程式人生 > >Oracle 自定義異常

Oracle 自定義異常

/*自定義異常:如果你想在某個特定時間發生時間嚮應用程式的使用者發出一些警告資訊。
而事件本身不會丟擲Oracle內部異常,這個異常是屬於應用程式的特定異常,那麼就需要自定義異常

使用者定義的異常錯誤是通過顯式使用raise語句來觸發,當引發一個異常錯誤時,控制就轉向到exception塊
異常錯誤部分,執行錯誤處理程式碼
步驟:
  1.在宣告部分定義異常  <異常情況> exception;
  2.raise <異常情況>
  3.異常情況處理部分對異常情況作出相應的處理
*/
declare
  v_empno emp.empno%type:=&empno;
  ex_result exception;
begin
  update emp set sal = sal + 100 where empno = v_empno;
  if sql%notfound then
    raise ex_result;
  else
    commit;
  end if;
  exception
    when ex_result then
      dbms_output.put_line('資料更新失敗!');
      rollback;
    when others then
      dbms_output.put_line('其他異常!');
end;