一:基本介绍
异常(EXCEPTION)类型:
1.预定义(Predefined)错误 常用的:没有结果:No_data_found 输出的行太多:Too_many_rows
2.非预定义(UnPredefined)错误
3.用户定义(User_define)错误
异常的格式:
exception
when Too_many_rows then dbms_output.put_line('输出的行数太多了');
二:错误处理
1.预定义异常
[预定义异常] 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;
2.非预定义异常
(1)声明异常 delete_mgr_excep exception;
(2)把自定义的错误和oracle的错误编号关联起来,将异常的编号自动转换为定义的异常
PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292);
(3)处理异常
when delete_mgr_excep then dbms_output.put_line('Manager不能直接被删除');
[非预定义异常] 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;
3.用户定义异常
(1)声明一个异常 too_high_sal exception;
(2)抛出一个异常 raise too_high_sal;
(3)处理异常 when too_high_sal then dbms_output.put_line('工资过高了');
[用户自定义异常] declare v_sal employees.salary%type; --声明一个异常 delete_mgr_excep exception; --把自定义的异常和oracle的错误关联起来 PRAGMA EXCEPTION_INIT(delete_mgr_excep,-2292); --(1)声明一个异常 too_high_sal exception; begin select salary into v_sal from employees where employee_id =100; if v_sal > 1000 then
--(2)抛出一个异常 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不能直接被删除'); --(3)处理异常 when too_high_sal then dbms_output.put_line('工资过高了'); end;