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-PyQt5-图形可视化界面(5)--打开文件或文件夹--QFileDialog
    python opencv图片拼接源码
    python numpy库矩阵运算的功能
    stm32定时器/定时器中断/PWM输出/输入捕获
    STM32 串口/中断
    STM32F4 IO
    STM32F4 时钟树概述
    MDK5新建工程/MDK5 使用技巧/STM32F4 在线调试
    使用 LocalDate 过滤掉工作日
    斐波拉契数列(Lambda表达式)
  • 原文地址:https://www.cnblogs.com/cbpm-wuhq/p/11958301.html
Copyright © 2011-2022 走看看