Quiz 7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7
At a glance
Powered by AI
The key takeaways from the document are that PL/SQL allows for exception handling to gracefully handle errors. There are predefined Oracle exceptions as well as user-defined exceptions that can be raised and handled. Exception handlers use the WHEN clause to specify the type of exception being handled.

The different types of exceptions in PL/SQL include predefined Oracle exceptions for errors like NO_DATA_FOUND, TOO_MANY_ROWS, etc. User-defined exceptions can also be declared and raised for application-specific errors.

To define an exception handler in PL/SQL, you use an EXCEPTION block with a WHEN clause to specify the exception being handled, such as WHEN NO_DATA_FOUND. Code inside the handler is executed when the specified exception is raised.

1.

 An attempt to update an employee's salary to a negative value will violate


a check constraint and raise an ORA-02290 exception. Which of the following
is a correct definition of a handler for this exception?
Mark for Review

(1) Points
DECLARE
    e_sal_excep EXCEPTION;
    PRAGMA_EXCEPTION_INIT(e_sal_exception,-02290);
DECLARE
    e_sal_excep EXCEPTION;
    PRAGMA EXCEPTION_INIT(e_sal_excep,02290);
DECLARE
    e_sal_excep EXCEPTION;
    PRAGMA EXCEPTION_INIT(-02290,e_sal_excep);
DECLARE
    e_sal_excep EXCEPTION;
    PRAGMA EXCEPTION_INIT(e_sal_excep,-02290); (*)
DECLARE
    PRAGMA EXCEPTION_INIT(e_sal_excep,-02290);
    e_sal_excep EXCEPTION;
Correct

2. A PL/SQL block executes and an Oracle Server exception is raised. Which
of the following contains the text message associated with the exception?
Mark for Review

(1) Points
SQL_MESSAGE_TEXT
SQLERRM (*)
SQL%MESSAGE
SQLCODE
Correct

3. Which one of the following events would implicitly raise an exception?


Mark for Review

(1) Points
A database constraint is violated. (*)
A SELECT statement returns exactly one row.
The PL/SQL programmer mis-spells the word BEGIN as BEGAN.
An UPDATE statement modifies no rows.
Correct

4. Which of the following is NOT a predefined Oracle Server error?


Mark for Review

(1) Points
DUP_VAL_ON_INDEX
ZERO_DIVIDE
NO_DATA_FOUND
TOO_MANY_ROWS
e_sal_too_high EXCEPTION; (*)
Correct

5. Which of the following best describes a predefined Oracle Server error?


Mark for Review

(1) Points
Has a standard Oracle error number and a standard name which can be
referenced in the EXCEPTION section (*)
Has a standard Oracle error number but must be named by the PL/SQL
programmer
Is associated with an Oracle error number using PRAGMA EXCEPTION_INIT

Is not raised automatically but must be declared and raised explicitly by the
PL/SQL programmer
Correct
6. The following line of code is correct. True or False?
RAISE_APPLICATION_ERROR(-21001,'My error message');
Mark for Review

(1) Points
True
False (*)
Correct

7. What is wrong with the following code?


BEGIN
    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;

Mark for Review

(1) Points
Line A should be: HANDLE NO_DATA_FOUND
You cannot use SQL%ROWCOUNT in conditional control statements such as
IF or CASE.
You cannot explicitly raise predefined Oracle Server errors such as
NO_DATA_FOUND.
Nothing is wrong; the code will execute correctly. (*)
NO_DATA_FOUND has not been DECLAREd.
Correct

8. You want to display your own error message to the user. What is the
correct syntax to do this?
Mark for Review

(1) Points
RAISE_APPLICATION_ERROR(20001, 'My own message');
RAISE application_error;
RAISE_APPLICATION_ERROR('My own message', -20001);
RAISE_APPLICATION_ERROR (-20001, 'My own message'); (*)
Correct

9. No employees are in department 99. What message or messages will be


displayed when the following code is executed?
DECLARE
    e_my_excep EXCEPTION;
BEGIN
    BEGIN
       UPDATE employees
       SET salary = 10000
          WHERE department_id = 99;
       IF SQL%ROWCOUNT = 0 THEN
          RAISE e_my_excep;
       END IF;
    EXCEPTION
       WHEN e_my_excep THEN
          DBMS_OUTPUT.PUT_LINE('Message 1');
          RAISE e_my_excep;
          DBMS_OUTPUT.PUT_LINE('Message 2');
    END;
    DBMS_OUTPUT.PUT_LINE('Message 3');
EXCEPTION
    WHEN e_my_excep THEN
       DBMS_OUTPUT.PUT_LINE('Message 4');
END;

Mark for Review

(1) Points
Message 1
Message 3
Message 4
Message 1
Message 2
Message 1
Message 4 (*)
Message 1
Message 3
Correct

10. What will happen when the following code is executed?


DECLARE
    e_excep1 EXCEPTION;
    e_excep2 EXCEPTION;
BEGIN
    RAISE e_excep1;
EXCEPTION
    WHEN e_excep1 THEN
         BEGIN
             RAISE e_excep2;
         END;
END;

Mark for Review

(1) Points
It will compile successfully and return an unhandled e_excep2 to the calling
environment. (*)
It will fail to compile because you cannot declare more than one exception in
the same block.
It will fail to compile because you cannot have a subblock inside an
exception section.
It will fail to compile because e_excep1 is out of scope in the subblock.
Correct
11. No employees exist in department 75. What will be displayed when this
code is executed?
DECLARE
    v_last_name employees.last_name%TYPE;
BEGIN
    DBMS_OUTPUT.PUT_LINE('A');
    BEGIN
       SELECT last_name INTO v_last_name
          FROM employees WHERE department_id = 75;
       DBMS_OUTPUT.PUT_LINE('B');
    END;
    DBMS_OUTPUT.PUT_LINE('C');
EXCEPTION
    WHEN OTHERS THEN
       DBMS_OUTPUT.PUT_LINE('D');
END;

Mark for Review

(1) Points
A
D (*)
A
C
D
None of these.
A
A
B
D
Correct

12. Which of the following best describes a PL/SQL exception?


Mark for Review

(1) Points
An error occurs during the execution of the block, which disrupts the normal
operation of the program. (*)
A user enters an invalid password while trying to log on to the database.
The programmer forgets to declare a cursor while writing the PL/SQL code.

A compile-time error occurs because the PL/SQL code references a non-


existent table.
Correct

13. Examine the following code. Why does this exception handler not follow
good practice guidelines? (Choose two.)
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;

Mark for Review

(1) Points
The exception handler should COMMIT the transaction.
department_id 75 does not exist in the departments table.
The exception section should include a WHEN TOO_MANY_ROWS exception
handler. (*)
You should not use DBMS_OUTPUT.PUT_LINE in an exception handler.
The exception handler should test for the named exception
NO_DATA_FOUND. (*)
Correct
14. Which of the following best describes a PL/SQL exception?
Mark for Review

(1) Points
An error occurs during execution which disrupts the normal operation of the
program. (*)
A user enters an invalid password while trying to log on to the database.
The programmer makes a spelling mistake while writiing the PL/SQL code.

A DML statement does not modify any rows.


Correct

15. Which of the following EXCEPTION sections is constructed correctly?


(Choose three.)
Mark for Review

(1) Points
EXCEPTION
    WHEN OTHERS THEN statement_1;
END; (*)
EXCEPTION
    WHEN TOO_MANY_ROWS THEN statement_1;
END; (*)
EXCEPTION
    WHEN OTHERS THEN statement_1;
    WHEN NO_DATA_FOUND THEN statement_2;
END;
EXCEPTION
    WHEN NO_DATA_FOUND THEN statement_1;
    WHEN OTHERS THEN statement_2;
END; (*)
EXCEPTION
    WHEN NO_DATA_FOUND THEN statement_1;
    WHEN NO_DATA_FOUND THEN statement_2;
    WHEN OTHERS THEN statement_3;
END;
Correct
Previous

You might also like