Oracle PL/SQL异常处理方法解析
Oracle数据库中的异常:没有异常的转移,因为没有受检异常和非受检异常得区分。
1.异常的产生:
2.异常的处理:
declare
--变量定义,初始化赋值。
begin
--变量的赋值,函数调用,if,while等。
exception
--异常处理代码
whenothersthen异常处理语句。
end;
3.异常的抛出:raise
4.多异常处理:Java的多异常是通过数据类型区分,Oracle数据库的多异常是通过异常编号区分。
区别不同的异常是实现多异常处理前提。
declare
verrorexception;--定义异常变量
PRAGMAEXCEPTION_INIT(verror,-111111);--设定异常变量的编号
begin
--变量的赋值,函数调用,if,while等。
exception
- whenverrorthen异常处理语句。--所抛出的异常变量的编号是否和设定好的异常变量的编号一致。
- whenothersthen异常处理语句。--当所抛出的异常编号在exception语句块中不存在时,执行该语句(写在最后)。
end;
5.自定义异常:Java中通过定义一个新的异常类实现的。Oracle中通过异常编号实现的。
eclare nnumber(1); v_errorexception; begin dbms_output.put_line('抛出单个异常练习--n只有1位不能保存数字10'); n:=10; ifn<=0then raisev_error; endif; dbms_output.put_line(n); exception whenothersthendbms_output.put_line('数值溢出'); end; declare nnumber(1); v_errorexception; PRAGMAEXCEPTION_INIT(v_error,-112122); begin dbms_output.put_line('抛出多个异常练习'); n:=-1; ifn<=0then raisev_error; endif; dbms_output.put_line(n); exception whenv_errorthendbms_output.put_line('不能为负'); whenothersthendbms_output.put_line('数值溢出'); end;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。