异常
1PL/SQL程序块在执行中出现的错误
2程序产生异常,如果没有处理语句,程序停止执行.
3异常的类型
1)预定义异常
2)非预定义异常
3)自定义异常
处理异常
exception
when exception1 then statements1;
when exception2 then statements2;
【…】
when others then statementN;
exception :表示出现的异常名称
when others:表示任何其他异常,类似else,该字句要放到最后
自定义异常
1用户自己在PL/SQL程序块,子程序或包中定义异常
2自定义异常需要在PL/SQL程序块的声明部分中进行声明
3oracle允许使用pragma关键字,让异常名和异常号连接起来.
4.用户自定义异常需要使用raise语句显示触发,也就是
raise exception_name,然后进行异常处理。
语法
exception_name exception
pragma exception_init(exception_name ,exception_no)
异常举例
declare
e_noDate_studemt exception;–自定义异常
pragma exception_init(e_noDate_studemt ,-1101);–让异常名和异常号连接起来
begin
update student set sname=’lihua’ where sno=’s01’;
IF SQL%notfound then –如果没有数据
raise e_noDate_studemt;–抛出异常
end if;
exception –异常处理
when e_noDate_studemt then
dbms_output.put_line(‘没有s01编号的学生’);
end;