您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > Oracle笔记-七、PLSQL-异常处理
--异常处理declaresNumnumber:=0;beginsNum:=5/sNum;dbms_output.put_line(sNum);exceptionwhenothersthendbms_output.put_line('isError!');end;--自定义异常declareex_custom_invaild_ageexception;--自定义的异常myerrageint;beginage:=&请输入年龄;if(age0)thenraiseex_custom_invaild_age;--引发自定义异常elsedbms_output.put_line('年龄是:'||age);endif;exceptionwhenex_custom_invaild_agethendbms_output.put_line('非法的年龄');end;--引发应用程序异常--raise_application_error(异常编号,说明);declareageint;beginage:=&请输入年龄;if(age0)thenraise_application_error(-20500,'年龄不能为负数');elsedbms_output.put_line('年龄是:'||age);endif;end;--非预定义异常declareex_custom_errorexception;pragmaexception_init(ex_custom_error,-1);--把一个编号和一个自定义异常关联,--相当于把-1编号的异常命名为ex_custom_error,这样就可以捕获这种异常begininsertintodeptvalues(10,'aaa','bbb');exceptionwhenex_custom_errorthendbms_output.put_line('部门编号已经存在');end;--异常处理declarevSalemp.sal%type;beginselectsalintovSalfromemp;exceptionwhentoo_many_rowsthendbms_output.put_line('多条数据');whenothersthendbms_output.put_line('Error');end;declarevSalemp.sal%type;beginselectsalintovSalfromempwhereempno=1;exceptionwhenno_data_foundthendbms_output.put_line('没有数据');whenothersthendbms_output.put_line('Error');end;--异常日志处理createtableerrorLog(idnumberprimarykey,errCodenumber,errMsgvarchar2(1024),errDatedate);--创建序列,从1开始,每次加1createsequenceseq_errorLog_idstartwith1incrementby1;declarevDeptnodept.deptno%type:=10;vErrCodenumber;vErrMsgvarchar2(1024);begindeletefromdeptwheredeptno=vDeptno;commit;exceptionwhenothersthenrollback;vErrCode:=SQLCODE;vErrMsg:=SQLERRM;insertintoerrorLogvalues(seq_errorLog_id.nextval,vErrCode,vErrMsg,sysdate);commit;end;select*fromerrorLog;
本文标题:Oracle笔记-七、PLSQL-异常处理
链接地址:https://www.777doc.com/doc-8637836 .html