以下plsql程序用的scott用戶的dept,emp表。
not_data_found例外:
--系統列外 set serveroutput on declare pename emp.ename%type; begin select ename into pename from emp where empno =1234; exception when no_data_found then dbms_output.put_line('沒有查到數據'); when others then dbms_output.put_line('其他'); end; /
too_many_rows例外:
1 --系統例外: too_many_rows 2 3 set serveroutput on 4 declare 5 6 pename emp.ename%type; 7 8 begin 9 10 select ename into pename from emp where deptno = 10; 11 12 exception 13 when too_many_rows then dbms_output.put_line('select into 匹配多行'); 14 when others then dbms_output.put_line('其他'); 15 end; 16 /
算數或轉換例外:
1 --系統例外 : value_error 2 3 set serveroutput on 4 5 declare 6 7 pnum number; 8 begin 9 pnum := 'abc'; 10 11 exception 12 when value_error then dbms_output.put_line('算術或轉換錯誤'); 13 when others then dbms_output.put_line('其他'); 14 end; 15 /
0不能做除數例外:
1 --系統例外 zero_divide 2 set serveroutput on 3 4 declare 5 6 pnum number; 7 begin 8 9 pnum := 1/0; 10 11 exception 12 when zero_divide then dbms_output.put_line('0不能做除數'); 13 when others then dbms_output.put_line('其他'); 14 end; 15 /
自定義例外:
--自定義例外: set serveroutput on declare cursor cemp is select ename from emp where deptno =50; pename emp.ename%type; --自定義列外 not_emp_data exception; begin open cemp; fetch cemp into pename; if cemp%notfound then raise not_emp_data; end if; --如果程序程序中出現例外,oracle會通過pmon(process monitor)自動關閉清理資源 close cemp; exception when not_emp_data then dbms_output.put_line('自定義例外:沒有查詢到數據'); when others then dbms_output.put_line('其他列外'); end; /
知識點出處:http://www.imooc.com/learn/360