PL/SQL 異常錯誤處理



異常錯誤處理
一個優秀的程序都應該能夠正確處理各種出錯情況,並儘可能從錯誤中恢復。ORACLE 提供異常情況(EXCEPTION)和異常處理(EXCEPTION HANDLER)來實現錯誤處理

①有三種類型的異常錯誤:
1. 預定義( Predefined )錯誤
    ORACLE預定義的異常情況大約有24個。對這種異常情況的處理,無需在程序中定義,由ORACLE自動將其引發。

2. 非預定義( Predefined )錯誤
    即其他標準的ORACLE錯誤。對這種異常情況的處理,需要用戶在程序中定義,然後由ORACLE自動將其引發。

3. 用戶定義(User_define) 錯誤
    程序執行過程中,出現編程人員認爲的非正常情況。對這種異常情況的處理,需要用戶在程序中定義,然後顯式地在程序中將其引發。

②異常處理部分一般放在PL/SQL 程序體的後半部,結構爲:
    EXCEPTION
        WHEN first_exception THEN<code to handle first exception >
        WHEN second_exception THEN<code to handle second exception >
        WHEN OTHERS THEN <code to handle others exception >
    END;
異常處理可以按任意次序排列,但OTHERS 必須放在最後.

③預定義的異常處理
對這種異常情況的處理,只需在PL/SQL塊的異常處理部分,直接引用相應的異常情況名,並對其完成相應的異常錯誤處理即可。

[預定義異常]
declare

  v_sal employees.salary%type;
begin
  select salary into v_sal
  from employees
  where employee_id >100;
 
  dbms_output.put_line(v_sal);

exception
  when Too_many_rows then dbms_output.put_line('輸出的行數太多了');
end;

④非預定義的異常處理
對於這類異常情況的處理,首先必須對非定義的ORACLE錯誤進行定義。步驟如下:
1. 在PL/SQL 塊的定義部分定義異常情況:
<異常情況>  EXCEPTION;

2. 將其定義好的異常情況,與標準的ORACLE錯誤聯繫起來,使用PRAGMA EXCEPTION_INIT 語句:
PRAGMA EXCEPTION_INIT(<異常情況>, <錯誤代碼>);

3. 在PL/SQL 塊的異常情況處理部分對異常情況做出相應的處理。

[非預定義異常]
declare

  v_sal employees.salary%type;
  --聲明一個異常
  delete_mgr_excep exception;
  --把自定義的異常和oracle的錯誤關聯起來
  PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
begin
  delete from employees
  where employee_id = 100;
 
  select salary into v_sal
  from employees
  where employee_id >100;
 
  dbms_output.put_line(v_sal);

exception
  when Too_many_rows then dbms_output.put_line('輸出的行數太多了');
  when delete_mgr_excep then dbms_output.put_line('Manager不能直接被刪除');
end;

⑤用戶自定義的異常處理
當與一個異常錯誤相關的錯誤出現時,就會隱含觸發該異常錯誤。用戶定義的異常錯誤是通過顯式使用RAISE語句來觸發。當引發一個異常錯誤時,控制就轉向到EXCEPTION塊異常錯誤部分,執行錯誤處理代碼。
對於這類異常情況的處理,步驟如下:
1. 在PL/SQL 塊的定義部分定義異常情況:

<異常情況>  EXCEPTION;
2. RAISE <異常情況>;

3. 在PL/SQL 塊的異常情況處理部分對異常情況做出相應的處理。

[用戶自定義異常]
declare

  v_sal employees.salary%type;
  --聲明一個異常
  delete_mgr_excep exception;
  --把自定義的異常和oracle的錯誤關聯起來
  PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
 
  --聲明一個異常
  too_high_sal exception;
begin

  select salary into v_sal
  from employees
  where employee_id =100;
 
  if v_sal > 1000 then
     raise too_high_sal;
  end if;
    
  delete from employees
  where employee_id = 100;

  dbms_output.put_line(v_sal);

exception
  when Too_many_rows then dbms_output.put_line('輸出的行數太多了');
  when delete_mgr_excep then dbms_output.put_line('Manager不能直接被刪除');
  --處理異常
  when too_high_sal then dbms_output.put_line('工資過高了');
end;

⑥在PL/SQL 中使用SQLCODE, SQLERRM

SQLCODE 返回錯誤代碼數字
SQLERRM 返回錯誤信息.

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE||’---‘||SQLERRM);


⑦異常程序:

1.通過 select ... into ... 查詢某人的工資, 若沒有查詢到, 則輸出 "未找到數據"
declare
  --定義一個變量
  v_sal employees.salary%type;
begin
  --使用 select ... into ... 爲 v_sal 賦值
  select salary into v_sal from employees where employee_id = 1000;
  dbms_output.put_line('salary: ' || v_sal);
exception
  when No_data_found then
       dbms_output.put_line('未找到數據');
end;


declare
  --定義一個變量
  v_sal employees.salary%type;
begin
  --使用 select ... into ... 爲 v_sal 賦值
  select salary into v_sal from employees;
  dbms_output.put_line('salary: ' || v_sal);
exception
  when No_data_found then
       dbms_output.put_line('未找到數據!');
  when Too_many_rows then
       dbms_output.put_line('數據過多!');    
end;

2. 更新指定員工工資,如工資小於300,則加100;對 NO_DATA_FOUND 異常, TOO_MANY_ROWS 進行處理.
declare
   v_sal employees.salary%type;
begin
   select salary into v_sal from employees where employee_id = 100;
  
   if(v_sal < 300) then
       update employees set salary = salary + 100 where employee_id = 100;
   else
       dbms_output.put_line('工資大於300');
   end if;
exception
   when no_data_found then
       dbms_output.put_line('未找到數據');
   when too_many_rows then
       dbms_output.put_line('輸出的數據行太多');
end;

3. 處理非預定義的異常處理: "違反完整約束條件"

declare
  --1. 定義異常 
  temp_exception exception;

  --2. 將其定義好的異常情況,與標準的 ORACLE 錯誤聯繫起來,使用 EXCEPTION_INIT 語句
  PRAGMA EXCEPTION_INIT(temp_exception, -2292);
begin
  delete from employees where employee_id = 100;

exception
  --3. 處理異常
  when temp_exception then
       dbms_output.put_line('違反完整性約束!');
end;

4. 自定義異常: 更新指定員工工資,增加100;若該員工不存在則拋出用戶自定義異常: no_result

declare
  --自定義異常                                  
  no_result exception;  
begin
  update employees set salary = salary + 100 where employee_id = 1001;

  --使用隱式遊標, 拋出自定義異常
  if sql%notfound then
     raise no_result;
  end if; 

exception

  --處理程序拋出的異常
  when no_result then
     dbms_output.put_line('更新失敗');
end;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章