1、系统定义异常:
异常是程序设计语言提供的一种功能,用来增强程序的健壮性和容错性。
no_data_fount | (没有找到数据) |
too_many_rows | (select ... into 语句匹配多个行) |
zero_divide | (被零除) |
value_error | (算术或转换错误) |
timeout_on_resource | (在等待资源时发生超时) |
例子:0不能做分母的异常
declare
pnum number;
begin
pnum := 1/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;
2、自定义异常,在声明中来定义异常
如果遇到异常我们要抛出异常,抛出异常的语句是 raise 异常名
例子:查询部门编号是50的员工姓名
declare
no_emp_found exception;
pename emp.ename%type;
cursor pemp is select t.ename from emp t where t.deptno=50;
begin
open pemp;
fetch pemp into pename;
if pemp%notfound then
--抛出自定义异常
raise no_emp_found;
end if;
close pemp;
exception
when no_emp_found then
dbms_output.put_line('没有找到员工');
when others then
dbms_output.put_line('其它错误');
end;