RSS

7.5.1 Understand How Exception Propagate

18 Apr

SET SERVEROUTPUT ON

DECLARE

    v_my_name VARCHAR2(15) := ‘ELENA SILVESTROVA’;

BEGIN

    DBMS_OUTPUT.PUT_LINE (‘My name is ‘||v_my_name);

    DECLARE

        v_your_name VARCHAR2(15);

    BEGIN

        v_your_name := ‘&sv_your_name’;

        DBMS_OUTPUT.PUT_LINE (‘Your name is ‘||v_your_name);

    EXCEPTION

        WHEN VALUE_ERROR THEN

            DBMS_OUTPUT.PUT_LINE (‘Error in the inner block’);

            DBMS_OUTPUT.PUT_LINE (‘This name is too long’);

    END;

EXCEPTION

    WHEN VALUE_ERROR THEN

         DBMS_OUTPUT.PUT_LINE (‘Error in the outer block’);

         DBMS_OUTPUT.PUT_LINE (‘This name is too long’);

END;

A) What exception is raised by the assignment statement in the declaration section of the outer block?

ANSWER: The exception VALUE_ERROR is raised by the assignment statement of the outer block. The variable v_my_name is declared as VARCHAR2(15). However, the value that is assigned to this variable contains 17 letters. As a result, the assignment statement causes a runtime error.

B) After this exception (based on the preceding question) is raised, will the program terminate successfully? Explain why or why not.

ANSWER: When the exception VALUE_ERROR is raised, the script cannot complete successfully because the error occurs in the declaration section of the outer block. Because the outer block is not enclosed by any other block, control is transferred to the host environment. As a result, an error message is generated when this example is run.

C) How would you change this script so that the exception can handle an error caused by the assignment statement in the declaration section of the outer block?

ANSWER: For the exception to handle the error generated by the assignment statement in the declaration section of the outer block, the assignment statement must be moved to the executable section of this block. All changes are shown in bold.

DECLARE

    v_my_name VARCHAR2(15);

BEGIN

v_my_name := ‘ELENA SILVESTROVA’;

 

Error in the outer block

This name is too long

Advertisements
 
Leave a comment

Posted by on 04/18/2010 in ORACLE

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: