You are on page 1of 11

N01580206 Yug Sutariya

academy.oracle.com

Database Programming with PL/SQL


7-4: Recognizing the Scope of Exceptions Practice
Activities
Vocabulary
Identify the vocabulary word for each definition below:

The inner block terminates unsuccessfully, and PL/SQL passes the


Propagation of Exceptions exception to the outer block.

The portion of the program where the exception can be accessed


Exception Visibility without using a qualifier.

The portion of a program in which the exception is declared and is


Exception Scope accessible.

Try It / Solve It
1. Enter and run the following code twice, once for each of the two country_ids, 5 (which does not
exist) and 672 (Antarctica, which does exist but has no currency).
DECLARE
v_country_name countries.country_name%TYPE;
v_currency_code countries.currency_code%TYPE;
BEGIN
DECLARE
e_no_currency EXCEPTION;
BEGIN
SELECT country_name, currency_code INTO v_country_name, v_currency_code
FROM countries
WHERE country_id = 5; -- repeat with 672
IF v_currency_code = 'NONE' THEN
RAISE e_no_currency;
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
N01580206 Yug Sutariya

END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('This country does not exist');
WHEN e_no_currency THEN
DBMS_OUTPUT.PUT_LINE('This country exists but has no currency');
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Another type of error occurred');
END;

A. Explain the output. Save your code.


Ans :- country_id=5;
N01580206 Yug Sutariya

Here , country id =5 is not there so it will throw exception and error to handle that error we have
exception to handle error and we are displaying message country doesn’t exist .

Country_id=672;
N01580206 Yug Sutariya

Here , First we have raise user-defined exception in executable section if country id exists but no
currency code so to handle that error we also have another exception and if that error occurs then
it will display message this country exists but has no currency.
N01580206 Yug Sutariya

B. Modify the code to move the two exception handlers to the outer block. Leave the declaration of
e_no_currency in the inner block. Execute twice, again using country_ids 5 and 672. Now what
happens and why? Save your code.

Ans :_

Here , error will occur because e_no_currency is declared in inner


block and in outer block it will show as not declared .

DECLARE
v_country_name countries.country_name%TYPE;
v_currency_code countries.currency_code%TYPE;
BEGIN
DECLARE
e_no_currency EXCEPTION;
BEGIN
SELECT country_name, currency_code INTO v_country_name, v_currency_code
FROM countries
WHERE country_id = 672; -- repeat with 672
IF v_currency_code = 'NONE' THEN
RAISE e_no_currency;
END IF;
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('This country does not exist');
WHEN e_no_currency THEN
DBMS_OUTPUT.PUT_LINE('This country exists but has no currency');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Another type of error occurred');
END;

Country id = 5;
N01580206 Yug Sutariya

Country_id=672;
N01580206 Yug Sutariya

C. Modify the code again to move the declaration of e_no_currency to the outer block. Execute the
code again using country_ids 5 and 672. Now what happens and why

Ans :-
N01580206 Yug Sutariya

Here , It is running properly and giving same output like question A by


simplying keeping ariable named e_no_currency as declared globally.

DECLARE

v_country_name countries.country_name%TYPE;

v_currency_code countries.currency_code%TYPE;

e_no_currency EXCEPTION;

BEGIN

BEGIN

SELECT country_name, currency_code INTO v_country_name, v_currency_code

FROM countries

WHERE country_id = 5; -- repeat with 672

IF v_currency_code = 'NONE' THEN

RAISE e_no_currency;

END IF;

END;

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE('This country does not exist');

WHEN e_no_currency THEN

DBMS_OUTPUT.PUT_LINE('This country exists but has no currency');

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE('Another type of error occurred');

END;

Country _id =5;


N01580206 Yug Sutariya

Country_id=672;
N01580206 Yug Sutariya
N01580206 Yug Sutariya
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

You might also like