zoukankan      html  css  js  c++  java
  • pl/sql进阶--例外处理

    在pl/sql的执行过程中发生异常时系统所作的处理称为一个例外情况(exception)。通常例外情况的种类有三种:

    1、预定义的oracle例外情况oracle预定义的例外情况大约有24个,对于这种例外情况无需在程序中定义,由oracle自动地触发(重点)

    2、非预定义的oracle例外情况由使用者增加定义例外情况,然后oracle自动将其触发执行。

    3、自定义例外,这个用的较少。

    自定义例外的基本语法:

    exception

        when <异常情况名> then

            <异常处理代码>

        when <异常情况名> then

            <异常处理代码>

        ...

        when others then

            <异常处理代码>

    捕获异常的两个目的:

    1、给用户提示更加明确,方便对过程优化;

    2、可能需要对异常进行业务处理。

    例外传递

    如果不处理例外我们看看会出现什么情况

    案例:编写一个过程,可接收雇员的编号,并显示该雇员的姓名。

    问题是,如果输入的雇员编号不存在,怎样去处理呢?

    create or replace procedure inempno_listename(v_in_empno number) is

    v_ename emp.ename%type;

    begin

        select ename into v_ename from emp where empno=v_in_empno;

    exception

        when no_data_found then

            dbms_output.put_line('你输入的雇员编号不存在!');

        when others then

            dbms_output.put_line('错误不明!');

    end;

    常用的预定义的例外

    oracle预定之例外情况的处理,下列出常见的几个:

    例外情况

    错误代码

    NO_DATA_FOUND

    ORA-01403

    TOO_MANY_ROWS

    ORA-01427

    INVALID_CURSOR

    ORA-01001

    VALUE_ERROR

    ORA-06502

    INVALID_NUMBER

    ORA-01722

    ZERO_DIVIDE

    ORA-01476

    DUP_VAL_ON_INDEX ORA-00001

    试图向具有唯一键值的索引中插入一个重复键值。

    CASE_NOT_FOUND

    ORA-06592

    CURSOR_NOT_OPEN ORA-06511

    游标没有打开

    预定义说明的部分ORACLE异常错误

    错误号

    异常错误信息名称

    说明

    ORA-0001

    Dup_val_on_index

    违反了唯一性限制

    ORA-0051

    Timeout-on-resource

    在等待资源时发生超时

    ORA-0061

    Transaction-backed-out

    由于发生死锁事务被撤消

    ORA-1001

    Invalid-CURSOR

    试图使用一个无效的游标

    ORA-1012

    Not-logged-on

    没有连接到ORACLE

    ORA-1017

    Login-denied

    无效的用户名/口令

    ORA-1403

    No_data_found

    SELECT INTO没有找到数据

    ORA-1422

    Too_many_rows

    SELECT INTO 返回多行

    ORA-1476

    Zero-divide

    试图被零除

    ORA-1722

    Invalid-NUMBER

    转换一个数字失败

    ORA-6500

    Storage-error

    内存不够引发的内部错误

    ORA-6501

    Program-error

    内部错误

    ORA-6502

    Value-error

    转换或截断错误

    ORA-6504

    Rowtype-mismatch

    宿主游标变量与 PL/SQL变量有不兼容行类型

    ORA-6511

    CURSOR-already-OPEN

    试图打开一个已处于打开状态的游标

    ORA-6530

    Access-INTO-null

    试图为null 对象的属性赋值

    ORA-6531

    Collection-is-null

    试图将Exists 以外的集合( collection)方法应用于一个null pl/sql 表上或varray上

    ORA-6532

    Subscript-outside-limit

    对嵌套或varray索引得引用超出声明范围以外

    ORA-6533

    Subscript-beyond-count

    对嵌套或varray 索引得引用大于集合中元素的个数.

    pl/sql进阶--例外处理

    预定义例外case_not_found

    在开发pl/sql块中编写case语句时,如果在when子句中没有包含必须的条件分支,就会触发case_not_found的例外。

    预定义例外zero_divide

    当执行2/0语句时,则会触发该例外。

    预定义例外no_data_found

    下面是一个pl/sql块,当执行select into 没有返回行,就会被触发该例外。

    预定义例外too_many_rows

    当执行select into语句时,如果返回超过了一行,则会触发该例外。

    案例:

    --cast_not_found案例

    create or replace procedure sp_pro6(spno number) is

        v_sal emp.sal%type;

    begin

        select sal into v_sal from emp where empno=spno;

    case

        when v_sal<1000 then

        update emp set sal=sal+100 where empno=spno;

        when v_sal<2000 then

        update emp set sal=sal+200 where empno=spno;

    end case;

    exception

        when case_not_found then

        dbms_output.put_line('case语句没有与'||v_sal||'相匹配的条件');

    end;

    --no_data_found案例

    declare

        v_sal emp.sal%type;

    begin

        select sal into v_sal from emp

        where ename='&name';

    exception

        when no_data_found then

        dbms_output.put_line('不存在该员工');

    end;

    --too_many_rows错误案例

    declare

        v_ename emp.ename%type;

    begin

        select ename into v_ename from emp;

    exception

        when too_many_rows then

        dbms_output.put_line('返回了多行');

    end;

    如何处理多个例外:

    set serveroutput on;

    declare

        var_name varchar(60);

    begin

        select ename into var_name from emp

        where deptno=&deptno;

    exception

        when no_data_found then

            dbms_output.put_line('没有匹配数据!');

        when too_many_rows then

            dbms_output.put_line('返回多行数据!');

        when others then

            dbms_output.put_line('提示错误不明!');

    end;

  • 相关阅读:
    MongoDB 之 手把手教你增删改查 MongoDB
    MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB
    全栈12期的崛起之捡点儿有用的说说
    Python 常用模块
    Python3中的内置函数
    Python程序员之面试必回习题
    Django之初始庐山真面目
    Django之ORM操作
    MySQL-索引
    MySQL-函数
  • 原文地址:https://www.cnblogs.com/roger112/p/7742132.html
Copyright © 2011-2022 走看看