oracle(39)_PL/SQL_ 例外

版权声明:如需转载,请注明出处 https://blog.csdn.net/qq_36260974/article/details/89053750

PL/SQL

例外

例外
  • 例外是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性。

  • 系统定义例外

    no_data_found    (没有找到数据)
    too_many_rows    (selectinto语句匹配多个行) 
    zero_divide      ( 被零除)
    value_error      (算术或转换错误)
    timeout_on_resource   (在等待资源时发生超时)
    
  • 范例:写出被 0 除的例外的 plsql 程序

  • 示例图:
    在这里插入图片描述
    在这里插入图片描述

  • 用户也可以自定义例外,在声明中来定义例外

  • 范例:查询部门编号是 50 的员工

  • 示例图:
    在这里插入图片描述
    在这里插入图片描述

● 以上操作完整源码:

--被零除
declare
  pnum number(4) := 5;
  
begin
  pnum := pnum / 0; --发生异常
  
exception
  ---处理异常
  when zero_divide then
    dbms_output.put_line('被0除');
  when value_error then
    dbms_output.put_line('数值转换错误');
  when others then
    dbms_output.put_line('其他异常');
end;

---查询部门编号是 50 的员工
declare
  prec emp%rowtype;
  cursor c1 is
    select * from emp t where t.deptno = 50;
  no_data exception; --异常类型的定义
  
begin
  open c1;
  loop
    fetch c1
      into prec;
    if c1%notfound then
      raise no_data; --抛出异常
    end if;
  end loop;
  close c1;
  
exception
  --处理异常
  when no_data then
    dbms_output.put_line('没有员工');
  when others then
    dbms_output.put_line('其他异常');
end;

如有错误,欢饮指正!

猜你喜欢

转载自blog.csdn.net/qq_36260974/article/details/89053750
今日推荐