zoukankan      html  css  js  c++  java
  • Oracle笔记 七、PL/SQL 异常处理

    --异常处理
    declare
      sNum number := 0;
    begin
      sNum := 5 / sNum;  
      dbms_output.put_line(sNum);
    exception 
      when others then
        dbms_output.put_line('is Error!');
    end;
     
    --自定义异常
    declare
           ex_custom_invaild_age exception; --自定义的异常myerr
           age int;
    begin
         age := &请输入年龄;
         if (age < 0) then
            raise ex_custom_invaild_age; --引发自定义异常
         else
            dbms_output.put_line('年龄是:' || age);     
         end if;
    exception 
          when ex_custom_invaild_age then
            dbms_output.put_line('非法的年龄');
    end;
     
    --引发应用程序异常
    --raise_application_error(异常编号,说明);
    declare        
           age int;
    begin
         age := &请输入年龄;
         if (age < 0) then
            raise_application_error(-20500, '年龄不能为负数');
         else
            dbms_output.put_line('年龄是:' || age);
         end if;
    end;
     
    --非预定义异常
    declare     
         ex_custom_error exception;
         pragma exception_init(ex_custom_error, -1); --把一个编号和一个自定义异常关联,
         --相当于把-1编号的异常命名为ex_custom_error,这样就可以捕获这种异常
    begin       
         insert into dept values(10, 'aaa', 'bbb');
         exception       
           when ex_custom_error then
           dbms_output.put_line('部门编号已经存在');
    end;
     
    --异常处理
    declare
      vSal emp.sal%type;
    begin
      select sal into vSal from emp;
      exception
        when too_many_rows then
          dbms_output.put_line('多条数据');
        when others then
          dbms_output.put_line('Error');
    end;
     
    declare
      vSal emp.sal%type;
    begin
      select sal into vSal from emp where empno = 1;
      exception
        when no_data_found then
          dbms_output.put_line('没有数据');
        when others then
          dbms_output.put_line('Error');
    end;
     
    --异常日志处理
    create table errorLog (
           id number primary key,
           errCode number,
           errMsg varchar2(1024),
           errDate date
    );
    --创建序列,从1开始,每次加1
    create sequence seq_errorLog_id start with 1 increment by 1;
     
    declare
           vDeptno dept.deptno%type := 10;
           vErrCode number;
           vErrMsg varchar2(1024);
    begin
      delete from dept where deptno = vDeptno;
      commit;
      exception
        when others then
          rollback;
          vErrCode := SQLCODE;
          vErrMsg := SQLERRM;
          insert into errorLog values(seq_errorLog_id.nextval, vErrCode, vErrMsg, sysdate);
          commit;
    end;
     
    select * from errorLog;
  • 作者:hoojo
    出处:
    blog:http://blog.csdn.net/IBM_hoojo
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权所有,转载请注明出处 本文出自:
分享道版权所有,欢迎转载,转载请注明出处,谢谢
收藏
关注
评论
查看全文
  • 相关阅读:
    关于WM_CTLCOLOREDIT的处理的一些问题
    Duilib非官方更新贴~
    一个非常简单的返回局部字符数组的C语言程序, 请问其输出结果?
    更改Windows控制台默认缓冲区行数和宽度
    最新版Duilib在VS2012下编译错误的解决方法
    记C语言浮点数运算处理 "坑" 一则
    修改stb_image.c以让Duilib直接支持Ico格式的图标显示
    一个通过网络转换Ico到Png图片的小小程序(Ico2Png)
    编程调节Win7/Win8系统音量的一种方法
    分享一个最近研究的手机QQ3.0的协议(版本1.4)
  • 原文地址:https://www.cnblogs.com/hoojo/p/2035350.html
  • Copyright © 2011-2022 走看看