您好,欢迎访问一九零五行业门户网

异常处理

异常处理 一、预定义异常: dup_val_on_index:试图向唯一索引列插入重复值,相当于建立了一个唯一索引 然后再向这个索引的列中插入一条重复的数据就会异常。 invalid_cursors:试图进行非法的游标操作 invalid_number : 试图将字符串转换为数字 no_data_found
异常处理
一、预定义异常:
dup_val_on_index:试图向唯一索引列插入重复值,相当于建立了一个唯一索引 然后再向这个索引的列中插入一条重复的数据就会异常。
invalid_cursors:试图进行非法的游标操作
invalid_number : 试图将字符串转换为数字
no_data_found : select into 语句没有返回任何记录
too_many_rows : select into 语句返回超过1条记录
zero_divide : 试图除以 0
cursor_already_open : 试图打开一个已经打开的游标
others :包括所有异常
二、异常处理语法结构:
exception --异常开始
when 异常名1 then
--对应的异常处理
1、实例:
declare
newsal emp.sal%type;
begin
select sal into newsal from emp;
exception
when too_many_rows then --超过记录用too_many_rows
dbms_output.put_line('返回的记录太多了');
when others then --其他异常则输出
dbms_output.put_line('未知异常');
end;
三、自定义异常:
declare
newsal emp.sal%type;
myexp exception; --声明异常变量
begin
select sal into newsal from emp where rownum=1;
if newsal>500 then --情况出现时就调用异常
raise myexp;
end if;
exception
when too_many_rows then --超过记录用too_many_rows
dbms_output.put_line('返回的记录太多了');
when myexp then
dbms_output.put_line('工资不能超过500');
when others then --其他异常则输出
dbms_output.put_line('未知异常');
end;
其它类似信息

推荐信息