zoukankan      html  css  js  c++  java
  • [bbk5300]第17集 Chapter 08 Handling Exceptions(00)

    Objectives

    After completing this lesson,you should be able to do the following:

    • Define PL/SQL execeptions
    • Recognize unhandled exceptions
    • List and use different types of PL/SQL execption handlers
    • Trap unanticipated errors.
    • Describe the effect of exception propagation in nested blocks
    • Customize PL/SQL exception messages

    Agenda

    • Understanding PL/SQL exceptions
    • Trapping exceptions

    What is an exception ? 

    无任何异常处理模块
    DECLARE
            v_lname VARCHAR(35);
    BEGIN
            SELECT last_name INTO v_lname
            FROM employees
            WHERE first_name = 'John';
    
            DBMS_OUTPUT.PUT_LINE('John`s last name is :' || v_lname);
    END;
    
    /
    SQL> @1.sql
    DECLARE
    *
    ERROR at line 1:
    ORA-01422: exact fetch returns more than requested number of rows
    ORA-06512: at line 4
    添加异常处理模块
    SET SERVEROUT ON;
    DECLARE
            v_lname VARCHAR(35);
    BEGIN
            SELECT last_name INTO v_lname
            FROM employees
            WHERE first_name = 'John';
    
            DBMS_OUTPUT.PUT_LINE('John`s last name is :' || v_lname);
    EXCEPTION
            WHEN OTHERS THEN
                    DBMS_OUTPUT.PUT_LINE('ERROR OCCURED : returned more than one rows!');
    END;
    
    /
    SQL> @1.sql
    ERROR OCCURED : returned more than one rows!
    
    PL/SQL procedure successfully completed.
    添加标准化异常处理模块
    SET SERVEROUT ON;
    DECLARE
            v_lname VARCHAR(35);
    BEGIN
            SELECT last_name INTO v_lname
            FROM employees
            WHERE first_name = 'John';
    
            DBMS_OUTPUT.PUT_LINE('John`s last name is :' || v_lname);
    EXCEPTION
            WHEN TOO_MANY_ROWS THEN
                    DBMS_OUTPUT.PUT_LINE('Your select statement retrieved multiple rows.Consider using a cursor.!');
    END;
    
    /
    SQL> @1_1.sql
    Your select statement retrieved multiple rows.Consider using a cursor.!
    
    PL/SQL procedure successfully completed.
    添加万能异常处理句柄
    SET SERVEROUT ON;
    DECLARE
            v_lname VARCHAR(15);
    BEGIN
            SELECT last_name INTO v_lname
            FROM emp
            WHERE first_name = 'John';
    
            DBMS_OUTPUT.PUT_LINE('John`s last name is :' || v_lname);
    EXCEPTION
            WHEN TOO_MANY_ROWS THEN
                    DBMS_OUTPUT.PUT_LINE('Your select statement retrieved multiple rows.Consider using a cursor.!');
            WHEN OTHERS THEN
                    DBMS_OUTPUT.PUT_LINE('You meet an error!');
    END;
    
    /
    SQL> @1_1
    You meet an error!
    
    PL/SQL procedure successfully completed.

    Handling the exception : Example(up look)

    TOO_MANY_ROWS等异常类型总结.

    PL/SQL Block Structure

    DECLARE(optional)

      -Variables,cursors,user-defined exceptions

    BEGIN(mandatory)

      -SQL statements

      -PL/SQL statements

    EXCEPTION(optional)

      -Action to perform when execptions occur

    END;(mandatory)

    Understanding Exceptions with PL/SQL

    • An exception is a PL/SQL error that is raised during program execution.
    • An exception can be raised:
      • -Implicitly by the Oracle Server
      • -Explicitly by the program
    • An exception can be handled:
      • -By trapping it with a handler
      • -By propagating it to the calling enviroment

    Handing Exceptions

    Understanding Exceptions with PL/SQL

    • When an error occurs in PL/SQL,an exception is raised.The processing in the current PL/SQL block`s execution section halt,and control is transferred to the separate exception section of the current block,if one exists,to handle the exception.
    • You cannot return to that block after you finish handling the exception.Instead,control is passed to the enclosing block,if any.

     图解:

    示例证明:只要进入到异常处理模块,即使使用GOTO也不能回到正常PL/SQL块
    DECLARE
            v_lname VARCHAR2(15);
    BEGIN
            SELECT last_name INTO v_lname
            FROM emp
            WHERE first_name = 'John';
    
            DBMS_OUTPUT.PUT_LINE('John`s last name is :' || v_lname);
            <<welcomback>>
            DBMS_OUTPUT.PUT_LINE('Welcom back!');
    EXCEPTION
            WHEN TOO_MANY_ROWS THEN
                    DBMS_OUTPUT.PUT_LINE('Your select statement retrieved multiple rows.Consider using a cursor.');
            WHEN OTHERS THEN
                    DBMS_OUTPUT.PUT_LINE('1:You meet an error!');
                    --GOTO welcomback;
                    GOTO gohere;
                    DBMS_OUTPUT.PUT_LINE('2:You are to die!');
                    <<gohere>>
                    DBMS_OUTPUT.PUT_LINE('3:You will be ended!');
    END;
    
    /
  • 相关阅读:
    跨浏览器右键复制实现
    在eclipse里如何快速定位到某一行?
    为数据库中的表添加字段步骤
    一键安装openstack juno 之controller node.
    Linux的/etc/services文件的作用?
    docker and ssh issues
    yum change source repo centos共存安装sun jdk6和jdk7
    random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串
    OpenStack,ceph
    那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等)[zz]
  • 原文地址:https://www.cnblogs.com/arcer/p/3039762.html
Copyright © 2011-2022 走看看