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;
  • 相关阅读:
    SlideShowExtender制作相册
    Response.Redirect(),Server.Transfer(),Server.Execute()的区别
    虚方法,抽象类,多态性
    gridview获取当前行索引的方法
    AutoQueryTextBox(AjaxPro.dll)非常值得研究的javascript代码
    abstract & virtual & override & new比较(转)
    Asp.net技巧:gridview获取当前行索引的方法
    js 获取浏览器高度和宽度值
    深入理解abstract class和interface
    c++ 静态数据成员和静态成员函数
  • 原文地址:https://www.cnblogs.com/cbpm-wuhq/p/11958301.html
Copyright © 2011-2022 走看看