您现在的位置是:网站首页> 编程资料编程资料

Oracle PL/SQL异常处理方法解析_oracle_

2023-05-27 503人已围观

简介 Oracle PL/SQL异常处理方法解析_oracle_

Oracle数据库中的异常:没有异常的转移,因为没有受检异常和非受检异常得区分。

1.异常的产生:

2.异常的处理:

declare

  --变量定义,初始化赋值。

begin

  --变量的赋值,函数调用,if,while等。

exception

  --异常处理代码

  when others then 异常处理语句。

end;

3.异常的抛出:raise

4.多异常处理:Java的多异常是通过数据类型区分,Oracle数据库的多异常是通过异常编号区分。
区别不同的异常是实现多异常处理前提。

declare

  verror exception;--定义异常变量

  PRAGMA EXCEPTION_INIT(verror ,-111111);--设定异常变量的编号

begin

  --变量的赋值,函数调用,if,while等。

exception

  •   when verror then 异常处理语句。--所抛出的异常变量的编号是否和设定好的异常变量的编号一致。
  •   when others then 异常处理语句。--当所抛出的异常编号在exception语句块中不存在时,执行该语句(写在最后)。

end;

5.自定义异常:Java中通过定义一个新的异常类实现的。Oracle中通过异常编号实现的。

 eclare n number(1); v_error exception; begin dbms_output.put_line('抛出单个异常练习--n只有1位不能保存数字10'); n:=10; if n<=0 then raise v_error; end if; dbms_output.put_line(n); exception when others then dbms_output.put_line('数值溢出'); end; declare n number(1); v_error exception; PRAGMA EXCEPTION_INIT(v_error,-112122); begin dbms_output.put_line('抛出多个异常练习'); n:=-1; if n<=0 then raise v_error; end if; dbms_output.put_line(n); exception when v_error then dbms_output.put_line('不能为负'); when others then dbms_output.put_line('数值溢出'); end;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

-六神源码网