100% found this document useful (1 vote)
2K views6 pages

Section 7 Quiz

This document contains a 13 question quiz on PL/SQL exception handling concepts. The questions cover topics like: - Using exception handlers to handle specific exceptions - Best practices for exception handling - Raising user-defined vs. predefined exceptions - Using exception handlers in nested blocks - Accessing error information using SQLCODE

Uploaded by

syahndra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views6 pages

Section 7 Quiz

This document contains a 13 question quiz on PL/SQL exception handling concepts. The questions cover topics like: - Using exception handlers to handle specific exceptions - Best practices for exception handling - Raising user-defined vs. predefined exceptions - Using exception handlers in nested blocks - Accessing error information using SQLCODE

Uploaded by

syahndra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Section 7 Quiz

(Answer all questions in this section)

1. Using two nested blocks, a TOO_MANY_ROWS exception is raised within the inner
block. Which of the following exception handlers will successfully handle the Mark for Review
exception? (1) Points

All of these. (*)


WHEN OTHERS in either block
WHEN TOO_MANY_ROWS in either block
WHEN OTHERS in the inner block
WHEN TOO_MANY_ROWS in the inner block

Correct

2. Non-predefined Oracle Server errors (associated with Oracle error numbers by


PRAGMA EXCEPTION_INIT) can be declared and raised in inner blocks and handled in Mark for Review
outer blocks. True or False? (1) Points

True
False (*)

Correct

3. Which of the following are good practice guidelines for exception handling? (Choose
three.) Mark for Review
(1) Points

(Choose all correct answers)

Use an exception handler whenever there is any possibility of an error occurring.


(*)
Handle specific named exceptions where possible, instead of relying on WHEN
OTHERS. (*)
Include a WHEN OTHERS handler as the first handler in the exception section.
Allow exceptions to propagate back to the calling environment.
Test your code with different combinations of data to see what potential errors
can happen. (*)

Incorrect. Refer to Section 7 Lesson 1.

4. Which of these exceptions can be handled by an EXCEPTION section in a PL/SQL


block? Mark for Review
(1) Points

An attempt is made to divide by zero


A SELECT statement returns no rows
None of these.
Any other kind of exception that can occur within the block
All of these. (*)

Correct

5. Examine the following code. Why does this exception handler not follow good practice
guidelines? (Choose two.) Mark for Review
(1) Points
DECLARE
v_dept_name departments.department_name%TYPE;
BEGIN
SELECT department_name INTO v_dept_name FROM departments
WHERE department_id = 75;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('A select returned more than one row');
END;

(Choose all correct answers)

department_id 75 does not exist in the departments table.


The exception handler should test for the named exception NO_DATA_FOUND.
(*)
You should not use DBMS_OUTPUT.PUT_LINE in an exception handler.
The exception handler should COMMIT the transaction.
The exception section should include a WHEN TOO_MANY_ROWS exception
handler. (*)

Incorrect. Refer to Section 7 Lesson 1.


6. Which of
the Mark for Review
following (1) Points
best
describes
a PL/SQL
exception?

An error occurs during execution which disrupts the normal operation of


the program. (*)
A DML statement does not modify any rows.
The programmer makes a spelling mistake while writiing the PL/SQL code.
A user enters an invalid password while trying to log on to the database.

Correct
7. Examine the followiing code. Which exception handlers would successfully trap
the exception which will be raised when this code is executed? (Choose two.) Mark for Review
(1) Points
DECLARE
CURSOR emp_curs IS SELECT * FROM employees;
v_emp_rec emp_curs%ROWTYPE;
BEGIN
FETCH emp_curs INTO v_emp_rec;
OPEN emp_curs;
CLOSE emp_curs;
EXCEPTION ...
END;

(Choose all correct answers)

WHEN OTHERS (*)


WHEN NO_DATA_FOUND
WHEN INVALID_CURSOR (*)
WHEN CURSOR_NOT_OPEN
WHEN INVALID_FETCH

Incorrect. Refer to Section 7 Lesson 2.

8. Examine the following code. The UPDATE statement will raise an ORA-02291
exception. Mark for Review
(1) Points
BEGIN
UPDATE employees SET department_id = 45;
EXCEPTION
WHEN OTHERS THEN
INSERT INTO error_log_table VALUES (SQLCODE);
END;

What will happen when this code is executed?

The code will fail because we cannot use functions like SQLCODE directly
in a SQL statement. (*)
The code will execute and insert error number 02291 into error_log_table.
The code will fail because we access error message numbers by using
SQLERRNUM, not SQLCODE.
The code will fail because SQLCODE has not been declared.

Correct

9. Which type of exception MUST be explicitly raised by the PL/SQL programmer?


Mark for Review
(1) Points

All of these.
Predefined Oracle server errors such as TOO_MANY_ROWS
Non-predefined Oracle server errors such as ORA-01203
User-defined exceptions (*)

Incorrect. Refer to Section 7 Lesson 2.

10. Examine the following code fragment. At Line A, you want to raise an
exception if the fetched salary value is greater than 30000. How can you do Mark for Review
this? (1) Points

DECLARE
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary FROM employees
WHERE employee_id = 100;
IF v_salary > 30000 THEN
-- Line A
END IF;
...

Test for WHEN OTHERS in the exception section, because WHEN OTHERS
traps all exceptions.
Use RAISE_APPLICATION_ERROR to raise an exception explicitly. (*)
Test for WHEN VALUE_TOO_HIGH in the exception section.
Define an EXCEPTION variable and associate it with an Oracle Server
error number using PRAGMA EXCEPTION_INIT.

Correct
11. Examine the following code. At
Line A, you want to raise an Mark for Review
exception if the employee's (1) Points
manager_id is null. What kind
of exception is this?

DECLARE
v_mgr_id
employees.manager_id%TYPE;
BEGIN
SELECT manager_id INTO
v_mgr_id FROM employees
WHERE employee_id =
100;
IF v_mgr_id IS NULL THEN
-- Line A
END IF;
...

A NO_DATA_FOUND exception
A constraint violation
A predefined Oracle Server exception
A user-defined exception (*)
A non-predefined Oracle server exception

Correct

12. What is wrong with the following code?


Mark for Review
BEGIN (1) Points
UPDATE employees SET salary = 20000
WHERE job_id = 'CLERK';
IF SQL%ROWCOUNT = 0 THEN
RAISE NO_DATA_FOUND; -- Line A
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee was
updated');
END;

Nothing is wrong; the code will execute correctly.


(*)
You cannot use SQL%ROWCOUNT in conditional
control statements such as IF or CASE.
NO_DATA_FOUND has not been DECLAREd.
You cannot explicitly raise predefined Oracle Server
errors such as NO_DATA_FOUND.
Line A should be: HANDLE NO_DATA_FOUND

Correct

13. No employees are in department_id 99. What output will


be displayed when the following code is executed? Mark for Review
(1) Points
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count
FROM employees WHERE department_id = 99;
IF v_count = 0 THEN
RAISE NO_DATA_FOUND;
DBMS_OUTPUT.PUT_LINE('No employees found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Department 99 is
empty');
END;

The block will fail because you cannot explicitly


RAISE a predefined Oracle Server error such as
NO_DATA_FOUND
Department 99 is empty (*)
No employees found
No employees found Department 99 is empty

Incorrect. Refer to Section 7


Lesson 3.

14. A user-defined exception can be raised:


Mark for Review
A. In the declaration section (1) Points
B. In the executable section
C. In the exception section

A and C
B and C (*)
C
B
A and B

Correct

15. What is a user-defined exception?


Mark for Review
(1) Points

A predefined Oracle server exception such as


NO_DATA_FOUND.
An exception which is not raised automatically by
the Oracle server, but must be declared and raised
explicitly by the PL/SQL programmer. (*)
An exception handler which the user (the
programmer) includes in the EXCEPTION section.
An exception which has a predefined Oracle error
number but no predefined name.

Incorrect. Refer to Section 7


Lesson 3.

You might also like