“`
**–系统例外: no_data_found,to_many_rows,Zero_Divide(被零除),Value_error(算术或转换错误),Timeout_on_resource (在等待资源时发生超时)
–自定义例外 : 查询 不存在的记录**
set serveroutput on
declare
–定义光标(此查询没有数据)
cursor tano is select t.tano from cfg_fund t where t.fundcode = ‘1q’;
no_tano_found exception;
ptano cfg_fund.tano%type;
pnum number;
begin
open tano;
select t.tano into ptano from cfg_fund t ;
ptano := 1/0;
pnum := ‘abc’;
fetch tano into ptano;
if tano%notfound then
--抛出自定义例外
raise no_tano_found;
end if;
–关闭光标
–如果上面的语句出现例外的话,按照代码执行顺序是不会关闭光标的,此时oracle 自动启动 pmon(process monitor),自动释放历史遗留垃圾和资源(在此例中就是关闭光标了)
close tano;
exception
–上面2条语句,如果上面一句出现例外,则终止程序了(相当于后面的赋值语句被短路了).
when no_data_found then dbms_output.put_line(‘没有找到数据哦’);
when too_many_rows then dbms_output.put_line(‘select into 语句匹配到多个行’);
when Zero_Divide then dbms_output.put_line(‘0不能做除数’);
when Value_error then dbms_output.put_line(‘算术或者转换错误’);
–捕获自定义例外
when no_tano_found then dbms_output.put_line(‘没有找到tano’);
when others then dbms_output.put_line(‘其他例外’);
end;
/“`