Oracle-例外(异常)
1.除0异常(zero_divide)
declare
vi number;
begin
vi := 8/0;
--捕获异常
exception
when zero_divide then
dbms_output.put_line('发生除0异常');
end;
2.类型转换异常(value_error)
declare
vi number;
begin
vi := 'aaa';
--捕获异常
exception
when value_error then
dbms_output.put_line('发生类型转换异常');
end;
3.赋值数量异常
declare
vrow emp%rowtype;
begin
select * into vrow from emp;
--捕获异常
exception
when too_many_rows then
dbms_output.put_line('查询出多行记录,但是赋值数量发生异常');
end;
4.未查询到数据异常
declare
vrow emp%rowtype;
begin
select * into vrow from emp where empno=1234567;
--捕获异常
exception
when no_data_found then
dbms_output.put_line('未查询到记录');
end;
5.其他异常
declare
vi number;
begin
vi := 'aaa';
--捕获异常
exception
when others then
dbms_output.put_line('发生异常');
end;
6.自定义异常
declare
--声明游标
cursor vrows is select * from emp where empno=8888;
--声明一个变量
vrow emp%rowtype;
--声明自定义异常
no_emp exception;
begin
open vrows;
--取数据
fetch vrows into vrow;
--判断游标内是否有数据
if vrows%notfound then
--没有查询到数据,抛出异常
raise no_emp;
end if;
close vrows;
exception
when no_emp then
dbms_output.put_line('发生自定义异常');
end;
来源:CSDN
作者:赵昕彧
链接:https://blog.csdn.net/qq_40579464/article/details/103751743