PL/SQL异常捕获

1. 异常一:NO_DATA_FOUND

1 declare 
2 pename emp.ename%TYPE;
3 begin
4 select ename into pename from emp where empno=1234;
5 exception
6 when no_data_found then dbms_output.put_line('没有找到该员工');8 when others then dbms_output.put_line('其他异常');
9 end;

2. 异常二:too_many_rows

1 declare 
2 pename emp.ename%TYPE;
3 begin
4 select ename into pename from emp where deptno=10;
5 EXCEPTION
6 when too_many_rows then dbms_output.put_line('select into匹配了很多行');
7 when others then dbms_output.put_line('其他异常');
8 end;

3. 异常三:zero_divide (除数是0时的异常)

1 declare 
2 pnum number;
3 begin
4 pnum :=1/0;
5 EXCEPTION
6 when zero_divide then dbms_output.put_line('o不能做除数');
7 when others then dbms_output.put_line('其他');
8 end;

4. 异常四:value_error

1 declare
2 pnum number;
3 begin
4 pnum :='abc';
5 EXCEPTION
6 when value_error then dbms_output.put_line('算数或者转换错误');
7 when others then dbms_output.put_line('其他');
8 end;

5. 自定义一个异常:no-emp_found

 1 declare
 2 cursor cemp is select ename from emp where deptno=50;
 3 pename emp.ename%TYPE;
 4 no_emp_found exception;  --定义一个异常
 5 begin
 6 open cemp;
 7 fetch cemp into pename;
 8 if cemp%notfound then 
 9 raise no_emp_found;   --raise表示抛出异常
10 end if;
11 close cemp;
12 EXCEPTION
13 when no_emp_found then dbms_output.put_line('没有找到员工');
14 when others then dbms_output.put_line('其他异常');
15 end;

 

posted @ 2020-03-23 22:50  佐小白  阅读(417)  评论(0)    收藏  举报