第五部分:错误处理 *************************************************************************************** --------------------------------------------------------------------------------------- --案例01:使用预定义错误data_not_found(单变量) declare nm emp.ename%type; begin select ename into nm from emp where empno=&no; dbms_output.put_line('name: '||nm); exception when no_data_found then dbms_output.put_line('不存在这个员工!'); end; --案例02:预定义错误data_not_found(单变量) declare nm emp.ename%type; no emp.empno%type; begin select ename into nm from emp where empno=7788; dbms_output.put_line('name:'||nm); select empno into no from emp where empno=9999; /*语句是不存在的,执行时会跳到异常部分*/ dbms_output.put_line('number:'||no); exception when no_data_found then dbms_output.put_line('不存在这个员工!'); end; /*如果没有异常部分,则整个语句块都不能进行*/ --案例03:使用多个预定义错误 declare nm emp.ename%type; no emp.empno%type; begin select ename into nm from emp where empno=7788; dbms_output.put_line('name:'||nm); select empno into no from emp where sal<5000; /*语句是不存在的,执行时会跳到异常部分*/ dbms_output.put_line('number:'||no); exception when no_data_found then dbms_output.put_line('不存在这个员工!'); when too_many_rows then dbms_output.put_line('数据太多!'); end; --案例04:未知的错误信息 declare nm emp.ename%type; no emp.empno%type; begin select ename into nm from emp where empno=7788; dbms_output.put_line('name:'||nm); select empno into no from emp where sal<5000; /*语句是不存在的,执行时会跳到异常部分*/ dbms_output.put_line('number:'||no); exception when no_data_found then dbms_output.put_line('不存在这个员工!'); when too_many_rows then dbms_output.put_line('数据太多!'); when others then dbms_output.put_line('没有遇到过的错误!'); end; --案例05:自定义错误00 declare invalidcategory exception; category varchar2(10); begin category:='&category'; if category not in ('附件', '顶盖', '备件') then raise invalidcategory; else dbms_output.put_line(' 您输入的类别是:'|| category); end if; exception when invalidcategory then dbms_output.put_line('对不起,无法识别这个设备!'); end; --案例06:自定义错误01 /*高考时需要选择学校专业,如果填报的是这些专业'语文', '数学', '物理','化学', '生物',中间任意一个专业就提示:‘欢迎你填报我校的%专业,感谢你选这我校,祝你好运!’如果填报的不是我们学校的专业则提示错误:‘对不起,这个不是我们学校的专业!请查实后再填报’*/ declare notmajor exception; work varchar2(10); begin work:='&wk'; if work in ('语文', '数学', '物理','化学', '生物' ) then dbms_output.put_line(' 欢迎你填报我校的:'||work||'专业,感谢你选择我校,祝你好运!'); else raise notmajor; end if; exception when notmajor then dbms_output.put_line('对不起,这个不是我们学校的专业!请查实后再填报!'); end; --案例07:自定义错误02(增、删、改) create table test (sid int, sno int ); -------------增 declare rep exception; s_id int; begin s_id:=&id; if s_id in (1001, 2001, 3001) then insert into test values (s_id, '&no'); dbms_output.put_line('符合我们的要求!'); else raise rep; end if; exception when rep then dbms_output.put_line('不需要这样的值!'); end; ---------------改 declare rep exception; s_id int; begin s_id:=&id; if s_id in (1001, 2001, 3001) then update test set sno=2008 where sid=s_id; dbms_output.put_line('满足修改的条件!'); else raise rep; end if; exception when rep then dbms_output.put_line('对不起,不能修改!'); end; ------删 declare rep exception; s_id int; begin s_id:=&id; if s_id in (1001, 2001, 3001) then delete from test where sid=s_id; dbms_output.put_line('符合删除的条件!'); else raise rep; end if; exception when rep then dbms_output.put_line('这些不符合删除的要求!'); end; --案例08:处理非预定义的错误 /* 使用预定义的例外只能处理21个oracle错误!在PL/SQL开发中会遇到其它一些oracle错误! */ /* SQL> begin 2 update emp set deptno=&dno where empno=&eno; 3 end; 4 / 输入 dno 的值: 11 输入 eno 的值: 7788 原值 2: update emp set deptno=&dno where empno=&eno; 新值 2: update emp set deptno=11 where empno=7788; begin * ERROR 位于第 1 行: ORA-02291: 违反完整约束条件 (SCOTT.FK_DEPTNO) - 未找到父项关键字 ORA-06512: 在line 2 */ 为了提高PL/SQL的健壮性,需要我们合理的处理这些oracle错误!实施步骤如下: 定义列外------->关联例外和错误--------->引用例外 declare e_int exception; pragma exception_init(e_int,-2291); --和执行语句时报错的错误号一致 begin update emp set deptno=&dno where empno=&eno; exception when e_int then dbms_output.put_line('对不起,该部门不存在!请核对信息!'); end; ---执行结果为: /* SQL> declare 2 e_int exception; 3 pragma exception_init(e_int,-2291); --和执行语句时报错的错误号一致 4 begin 5 update emp set deptno=&dno where empno=&eno; 6 exception 7 when e_int then 8 dbms_output.put_line('对不起,该部门不存在!请核对信息!'); 9 end; 10 / 输入 dno 的值: 11 输入 eno 的值: 7788 原值 5: update emp set deptno=&dno where empno=&eno; 新值 5: update emp set deptno=11 where empno=7788; 对不起,该部门不存在!请核对信息! PL/SQL 过程已成功完成。 */ --案例09:处理预定义的例外 declare e_int exception; pragma exception_init(e_int,-2291); --和执行语句时报错的错误号一致 begin update emp set deptno=&dno where empno=&eno; exception when e_int then dbms_output.put_line('对不起,该部门不存在!请核对信息!'); end; --执行的结果为: /* SQL> 1 declare 2 e_int exception; 3 pragma exception_init(e_int,-2291); --和执行语句时报错的错误号一致 4 begin 5 update emp set deptno=&dno where empno=&eno; 6 exception 7 when e_int then 8 dbms_output.put_line('对不起,该部门不存在!请核对信息!'); 9* end; 输入 dno 的值: 10 输入 eno 的值: 1111 原值 5: update emp set deptno=&dno where empno=&eno; 新值 5: update emp set deptno=10 where empno=1111; PL/SQL 过程已成功完成。 */ 上面输入的值在表中不能存在但是oracle并没有报任何错误!为了给用户提供更加有用的信息,我们需要提示:该员工不存在 的信息! 步骤: 定义列外----->显示触发例外----->引用例外 declare e_int exception; pragma exception_init(e_int,-2291); --和执行语句时报错的错误号一致 e_no_employee exception; begin update emp set deptno=&dno where empno=&eno; if sql%notfound then raise e_no_employee; end if; exception when e_int then dbms_output.put_line('对不起,该部门不存在!请核对信息!'); when e_no_employee then dbms_output.put_line('该员工不存在!'); end;