zoukankan      html  css  js  c++  java
  • Oracle PL/SQL 异常处理

    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中通过异常编号实现的。

     1 eclare 
     2     n number(1);
     3     v_error exception;
     4 begin
     5     dbms_output.put_line('抛出单个异常练习--n只有1位不能保存数字10');
     6     n:=10;
     7     if n<=0 then
     8        raise v_error;
     9     end if;
    10     dbms_output.put_line(n);
    11 exception
    12     when others then dbms_output.put_line('数值溢出');
    13 end;
    14 
    15 declare 
    16     n number(1);
    17     v_error exception;
    18     PRAGMA EXCEPTION_INIT(v_error,-112122);
    19 begin
    20     dbms_output.put_line('抛出多个异常练习');
    21     n:=-1;
    22     if n<=0 then
    23        raise v_error;
    24     end if;
    25     dbms_output.put_line(n);
    26 exception
    27     when v_error then dbms_output.put_line('不能为负');
    28     when others then dbms_output.put_line('数值溢出');
    29 end;
  • 相关阅读:
    欧拉公式
    isap的一些想法
    错误合集
    Hello World
    PAT (Advanced Level) Practice 1068 Find More Coins
    PAT (Advanced Level) 1087 All Roads Lead to Rome
    PAT (Advanced Level) 1075 PAT Judge
    PAT (Advanced Level) 1067 Sort with Swap(0, i)
    PAT (Advanced Level) 1017 Queueing at Bank
    PAT (Advanced Level) 1025 PAT Ranking
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3829863.html
Copyright © 2011-2022 走看看