Difficulty: Medium
Correct Answer: Predefined PL/SQL exceptions such as NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, DUP_VAL_ON_INDEX, and VALUE_ERROR are named error handlers that Oracle raises automatically for common runtime errors like missing data, duplicate keys, divide by zero, and invalid conversions.
Explanation:
Introduction / Context:
Exception handling is a critical part of reliable PL/SQL programming. Oracle defines a set of predefined exceptions for common error conditions so that you do not need to declare them yourself. This question asks you to describe some of the most frequently used predefined PL/SQL exceptions and what they represent.
Given Data / Assumptions:
Concept / Approach:
Predefined PL/SQL exceptions are named identifiers bound to standard Oracle error numbers. Examples include NO_DATA_FOUND when a SELECT INTO finds no rows, TOO_MANY_ROWS when a SELECT INTO returns more than one row, ZERO_DIVIDE when an attempt is made to divide by zero, DUP_VAL_ON_INDEX when an INSERT or UPDATE violates a unique constraint, and VALUE_ERROR for conversion errors and some numeric overflows. Oracle raises these automatically when the corresponding condition is detected, and your PL/SQL code can catch them by name in the EXCEPTION section.
Step-by-Step Solution:
Step 1: Identify NO_DATA_FOUND, which is raised when a SELECT INTO statement returns no row even though the code expects one, often handled to provide default values or custom messages.Step 2: Identify TOO_MANY_ROWS, which is raised when a SELECT INTO returns more than one row, indicating that the query logic did not uniquely identify a single record.Step 3: Identify ZERO_DIVIDE, raised when an arithmetic expression attempts to divide by zero, which is mathematically undefined.Step 4: Identify DUP_VAL_ON_INDEX, raised when an INSERT or UPDATE causes a unique index or constraint violation, usually indicating duplicate keys.Step 5: Identify VALUE_ERROR, which occurs when conversions between data types fail or when numeric values exceed the allowed range, for example converting a long string into a shorter variable.
Verification / Alternative check:
You can test each exception by writing small PL/SQL blocks that deliberately trigger the error condition and catching the exception by name. For example, divide a number by zero to confirm ZERO_DIVIDE, or insert a duplicate value into a column with a unique constraint to see DUP_VAL_ON_INDEX. Observing the behaviour matches the documented meanings of these predefined exceptions.
Why Other Options Are Wrong:
Option B incorrectly states that predefined exceptions are never raised automatically, which contradicts Oracle behaviour. Option C claims they are only for SQL Plus scripts, but they are integral to PL/SQL itself. Option D suggests they do not represent real errors, which is incorrect; they directly correspond to serious runtime problems that must be handled or allowed to propagate.
Common Pitfalls:
Developers sometimes write WHEN OTHERS THEN blocks that swallow all exceptions without logging details, which hides specific predefined exceptions and makes diagnosis difficult. Another pitfall is failing to anticipate NO_DATA_FOUND in SELECT INTO statements. Good practice is to catch specific predefined exceptions when you can handle them cleanly and to log unexpected cases for investigation.
Final Answer:
Predefined PL/SQL exceptions such as NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, DUP_VAL_ON_INDEX, and VALUE_ERROR are named error handlers that Oracle raises automatically for common runtime errors like missing data, duplicate keys, divide by zero, and invalid conversions.
Discussion & Comments