异常是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性。
1.系统定义异常
no_data_found (没有找到数据)
too_many_rows (select …into语句匹配多个行)
zero_divide ( 被零除)
value_error (算术或转换错误)
timeout_on_resource (在等待资源时发生超时)
2.自定义异常
-- 查询50号部门的员工姓名
declare
-- Local variables here
cursor cemp is select ename from emp where deptno=50;
pename emp.ename%type;
--自定义例外
no_emp_found exception;
begin
open cemp;
fetch cemp into pename;
if cemp%notfound then
raise no_emp_found;
end if;
close cemp;
exception
when no_emp_found then dbms_output.put_line('没有找到员工');
when others then dbms_output.put_line('其他例外');
end;