0 定义:
a) 是一种功能,说白了就是异常
b) 增强程序健壮性和容错性
c) 功能类似于java的 try{} catch(){} finally{}
1 写法:
exception when xxx then xxx;
2 分类:
a) 系统定义例外
| no_data_found | 未找到数据 |
| too_many_rows | 查询语句匹配多行 |
| zero_error | 被0除 |
| value_error | 算数/转换错误 |
| timeout_on_resource | 等待资源时超时 |
案例: 被0除
set serveroutput on
declare
pnum number;
begin
pnum := 1/0;
exception
when Zero_Divide then dbms_output.put_line('1:0不能做被除数');
dbms_output.put_line('2:0不能做被除数');
when Value_error then dbms_output.put_line('算术或转换错误');
when others then dbms_output.put_line('其他例外');
end;
/
结果:
1:0不能做被除数
2:0不能做被除数
b) 自定义例外
set serveroutput on
declare
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;
/
数据字典概念:
是管理员提供的表,只能查不能改
数据字典命名规则:
| 前缀 | 说明 |
| USER | 用户自己的 |
| ALL | 用户自己可以访问的 |
| DBA | 管理员的 |
| V$ | 性能相关的数据 |
Oracle异常处理与数据字典
570

被折叠的 条评论
为什么被折叠?



