异常错误处理

一个优秀的程序都应该可以正确处理各种出错情况,并尽可能从错误中恢复。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;

最新文章

  1. Web报表工具FineReport的JS API开发(二)
  2. SQL Server 临时禁用和启用所有外键约束(高版本向低版本迁移数据)
  3. SQL Server提高事务复制效率优化(一)总体概述
  4. ansible 自动化(2)
  5. 《linux内核设计与实现》读书笔记第三章
  6. HNOI2017 滚粗记
  7. Elasticsearch学习笔记(五)索引元数据和集群元数据
  8. mysql知识点拾遗梳理
  9. 系统编码,文件编码,python编码
  10. jackson出现错误 Unrecognized field,几种处理方法
  11. #个人作业Week2——结对编程对象代码复审
  12. base64的编码
  13. Javascript中的Bind,Call和Apply
  14. Linux常见目录使用区别
  15. 【Python】TCP Socket的粘包和分包的处理
  16. Express搭建NodeJS项目
  17. Activiti Designer 5.14.1插件安装和使用
  18. jdbc第三天
  19. 把大象装进冰箱的N种方法
  20. annexb模式

热门文章

  1. Vue CLI3 关闭热替换后出现的warning
  2. UVa247
  3. CF A.Mishka and Contest【双指针/模拟】
  4. codeforces 868B The Eternal Immortality【暴力+trick】
  5. Oracle like 里面的通配符 以及regexp_like
  6. URAL 1682 Crazy Professor (并查集)
  7. [Contest20180116]随机游走
  8. 【二维树状数组】bzoj1452 [JSOI2009]Count
  9. Scala实战高手****第15课:Scala类型参数编程实战及Spark源码鉴赏
  10. js发送验证码(倒计时)