首页 > 代码库 > oracle plsql exception例外

oracle plsql exception例外

not_data_found例外:

--系统列外
set serveroutput on

declare

  pename emp.ename%type;
  
begin

  select ename into pename  from emp where empno =1234;
  
exception
  when no_data_found then dbms_output.put_line(没有查到数据);
  when others then dbms_output.put_line(其他);
  
end;
/

 

too_many_rows例外:

 1 --系统例外: too_many_rows
 2 
 3 set serveroutput on
 4 declare
 5 
 6   pename emp.ename%type;
 7 
 8 begin
 9 
10   select ename into pename from emp where deptno = 10;
11 
12 exception
13   when too_many_rows then dbms_output.put_line(select into 匹配多行);
14   when others  then dbms_output.put_line(其他);
15 end;
16 /

 

算数或转换例外:

 1 --系统例外 : value_error
 2 
 3 set serveroutput on
 4 
 5 declare
 6   
 7   pnum number;
 8 begin
 9   pnum := abc;
10   
11 exception
12   when value_error then dbms_output.put_line(算术或转换错误);
13   when others then dbms_output.put_line(其他);
14 end;
15 /

 

0不能做除数例外:

 1 --系统例外 zero_divide
 2 set serveroutput on
 3 
 4 declare
 5 
 6   pnum number;
 7 begin
 8 
 9   pnum := 1/0;
10   
11 exception
12   when zero_divide then dbms_output.put_line(0不能做除数);
13   when others then dbms_output.put_line(其他);
14 end;
15 /

 

自定义例外:

--自定义例外: 

set serveroutput on

declare

  cursor cemp is select ename from emp where deptno =50;
  pename emp.ename%type;

  --自定义列外
  not_emp_data exception;
  
begin
  open cemp;
    
  fetch cemp into pename;
  
  if cemp%notfound then
      raise not_emp_data;
  end if;
  --如果程序程序中出现例外,oracle会通过pmon(process monitor)自动关闭清理资源
  close cemp;  

exception 
  when not_emp_data then dbms_output.put_line(自定义例外:没有查询到数据);
  when others then dbms_output.put_line(其他列外);
end;
/

 

 

知识点出处:http://www.imooc.com/learn/360

oracle plsql exception例外