Oracle PL/SQL学习之基础篇(2)--例外

1、例外分类:系统例外、自定义例外

    (1)系统例外,参见相关API文档

    (2)自定义例外

         定义自己的例外:就像自定义变量一样,类型为exception

        抛出例外:使用raise抛出自定义例外

set serveroutput on
declare
    cursor cemp is select ename from emp where deptno=50;
    
    pename emp.ename%type;
    
    --self define exception
    self_no_data_found exception;
begin
    open cemp;
    
    fetch cemp into pename;
    if cemp%notfound then
        --throw self define exception
        raise self_no_data_found;
    end if;
exception
   when self_no_data_found then
      dbms_output.put_line('执行了自定义异常!');
      if cemp%isopen then
         dbms_output.put_line('close cursor!');
         close cemp;
      end if;
   when others then
      dbms_output.put_line('执行了others异常!');
      if cemp%isopen then
         close cemp;
      end if;
end;
/

运行结果:

猜你喜欢

转载自www.cnblogs.com/ZeroMZ/p/9326749.html
今日推荐