oracle(39)_PL/SQL_ 例外
PL/SQL
例外
● 例外
-
例外是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性。
-
系统定义例外
no_data_found (没有找到数据) too_many_rows (select …into语句匹配多个行) zero_divide ( 被零除) value_error (算术或转换错误) timeout_on_resource (在等待资源时发生超时)
-
范例:写出被 0 除的例外的 plsql 程序
-
示例图:
-
用户也可以自定义例外,在声明中来定义例外
-
范例:查询部门编号是 50 的员工
-
示例图:
● 以上操作完整源码:
--被零除
declare
pnum number(4) := 5;
begin
pnum := pnum / 0; --发生异常
exception
---处理异常
when zero_divide then
dbms_output.put_line('被0除');
when value_error then
dbms_output.put_line('数值转换错误');
when others then
dbms_output.put_line('其他异常');
end;
---查询部门编号是 50 的员工
declare
prec emp%rowtype;
cursor c1 is
select * from emp t where t.deptno = 50;
no_data exception; --异常类型的定义
begin
open c1;
loop
fetch c1
into prec;
if c1%notfound then
raise no_data; --抛出异常
end if;
end loop;
close c1;
exception
--处理异常
when no_data then
dbms_output.put_line('没有员工');
when others then
dbms_output.put_line('其他异常');
end;
如有错误,欢饮指正!