zoukankan      html  css  js  c++  java
  • plsql--异常处理

    一:基本介绍:plsql中的异常处理

    异常(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;
  • 相关阅读:
    python练习1--求和
    python--文件操作
    【省选】SDOI2017_树点涂色_LuoguP3703/LOJ2001_LCT/线段树
    【洛谷】P1784 数独
    【洛谷】P2671 求和
    【洛谷】P2261 [CQOI2007]余数求和
    【转载】利用向量积(叉积)计算三角形的面积和多边形的面积
    【洛谷】P1090 合并果子
    【转载】学习C++ -> 类(Classes)的定义与实现
    【洛谷】P2142 高精度减法
  • 原文地址:https://www.cnblogs.com/cbpm-wuhq/p/11958301.html
Copyright © 2011-2022 走看看