PL/SQL(四):异常

来源:互联网 发布:网络贷款影响征信吗 编辑:程序博客网 时间:2024/05/16 01:51

一、异常


       是在PL/SQL执行过程中出现的警告或错误,分为编译时错误和运行时错误


二、触发异常条件


      发生一个Oracle错误
      使用raise语句显示触发

三、异常类型


1)预定义异常
       异常情况名                            错误代码                        描述
       DUP_VAL_ON_INDEX    ORA-00001            试图更新或插入重复记录
       INVALID_CURSOR         ORA-01001            非法游标操作
       INVALID_NUMBER         ORA-01722            字符串向数字转换失败
       NO_DATA_FOUND         ORA-01403            执行的SELECT没有查到数据
      TOO_MANY_ROWS        ORA-01427            未使用游标,SELECT语句返回了多行数据

declare  cursor c1 is  select * from T_SCORE;  score_total real;  begin  score_total := 0.000;    for temp in c1 loop    score_total := score_total + temp.EXAM_SCORE;      end loop;  dbms_output.put_line(score_total);  dbms_output.put_line(3**5);end;



      VALUE_ERROR               ORA-06502           出现数字、数据转换、字符串或限制型错误
      ZERO_DIVIDE                  ORA-01476           被零除
2)非预定义异常
      自定义异常值与系统内置异常编号绑定,如
          e_emp_cons exception;     
                  pragma exception_init(e_emp_cons,-00001);
3)自定义异常
       完全自定义异常

 

四、示例代码

 

--1 预定义异常declare  v_score T_SCORE.EXAM_SCORE%type;  begin  select T_SCORE.EXAM_SCORE into v_score from T_SCORE  where T_SCORE.AUTOID = 10010001;      exception    when no_data_found then      dbms_output.put_line('没有查到数据');    when others then      dbms_output.put_line('其他异常');      end;


--2 非预定义异常declare  e_emp_cons exception;   --声明异常  pragma exception_init(e_emp_cons,-00001);  --跟系统内置异常编号绑定  begin  insert into T_SCORE     --选取T_SCORE全部记录,再插入T_SCORE表中(违反唯一性约束)    select * from T_SCORE;    exception    when e_emp_cons then      dbms_output.put_line('主键值有重复!');    when others then      dbms_output.put_line('其他异常');end;

--3 自定义异常declare  e_too_many exception;  e_too_low exception;  v_count number;  begin  select count(*) into v_count from T_SCORE  where T_SCORE.EXAM_SCORE > (select avg(T_SCORE.EXAM_SCORE) from T_SCORE);    dbms_output.put_line(v_count);    if(v_count>5) then    raise e_too_many;  else    raise e_too_low;  end if;      exception    when e_too_many then      dbms_output.put_line('大于5');    when e_too_low then      dbms_output.put_line('小于5');    when others then      dbms_output.put_line('其他异常');end;

--4 捕获异常的函数begin  insert into T_SCORE    select * from T_SCORE;    exception    when others then      dbms_output.put_line(sqlerrm);   -- ORA-00001: 违反唯一约束条件 (TEST.PK_T_SCORE)      dbms_output.put_line(sqlcode);   -- -1end;

--5 Raise_Application_Error,范围定义在-20000 到 -209999中-- 1)在执行部分抛出异常begin  delete from T_SCORE where T_SCORE.EXAM_SCORE>1990;  if SQL%NOTFOUND then    Raise_Application_Error(-20202,'没有这个成绩');  --抛出  end if;  dbms_output.put_line(1);   --不执行    exception    when others then      dbms_output.put_line('没有指定的成绩');      dbms_output.put_line(sqlerrm);   --错误消息      dbms_output.put_line(sqlcode);   --错误代码end;-- 2) 在异常区域抛出异常declare  emp_row T_SCORE%rowtype;begin  select * into emp_row from T_SCORE where T_SCORE.EXAM_SCORE>1990;  dbms_output.put_line(1);   --不执行  exception    when NO_DATA_FOUND then      Raise_Application_Error(-20111,'没有符合条件的成绩'); --抛出end;