plsql的exception用法

对exception的用户比较糊涂,专门记录总结一下,方便自己查看
plsql的异常处理通常是处理运行异常
声明异常:
1.用户自定义异常
declare e_myexception EXCEPTION;
2.系统预定义异常
常见:
no_data_found:出现在select into
invalid_cursor:非法的游标
value_error:转换,运算

抛出异常:与java异常抛出类似
raise myexception

处理异常:某个异常最多由异常部门一个处理器进行处理
exception
  when 异常名 then
   执行语句;
  when 异常名 then
   执行语句;
  when OTHERS THEN
   执行语句;
END;
others异常处理其他异常所不能捕获的异常
在others中记录具体错误 SQLCODE,SQLERRM
V_errcode := SQLCODE;
V_errortext := SQLERRM;
insert into log values (v_errcode,v_errortext);

exception_init编译指示
把某异常与特定的oracle错误关联起来
declare
    e_missingnull exception;
     pragma exception_init(e_missingnull,-1200);

raise_application_error创建自己的错误消息
raise_application_error(error_number,error_message,[keep_errors]);
error_number:-20000和-20999之间的数字
error_message:自定义小于512个字符
keep_errors:默认为false

异常的传播:
1.如果当前语句块有该异常处理器,执行,完成该语句块,控制权传递到外层
2.如果当前语句块没有该异常处理器,则通过外部语句块来传播该异常
3.声明部门异常,直接调到外层异常处理

猜你喜欢

转载自aigo-h.iteye.com/blog/1917535