一、
开发PL/SQL程序时,需要考虑到程序运行时可能出现的各种异常,当异常出现时,或是中断程序运行,或是使程序从错误中恢复,从而继续运行。
常用的异常类型有:
no_data_found:没有发现数据
too_many_rows:select into 语句查询结果有多个数据行
others:可以捕捉所有异常,一般作为异常处理部分的最后一个异常处理器
二、例子
-- v_code : 000 ,表示执行成功,其它表示执行失败
create or replace procedure detector_plsql_exception(
v_deptno varchar2,
v_dname out varchar2,
v_code out varchar2,
v_msg out varchar2
)
as
begin
select d.dname into v_dname from dept d where d.deptno = v_deptno;
v_code := '000';
exception
when no_data_found then
v_code := '001';
v_msg := '找不到deptno为'||v_deptno||'的记录';
when too_many_rows then
v_code := '002';
v_msg := 'deptno为'||v_deptno||'的记录多于一条';
when others then
v_code := '999';
v_msg := '其它异常,'||sqlcode||','||sqlerrm;
--sqlcode:当前错误代码
--sqlerrm:当前错误消息文件
end detector_plsql_exception;
..