Sobes.tech
Middle

Have you worked with exceptions? What types of exceptions are there in PL/SQL?

sobes.tech AI

Answer from AI

In PL/SQL, exception handling is an important part of error management and flow control.

Main types of exceptions in PL/SQL:

  1. Predefined exceptions — built into the language, for example:

    • NO_DATA_FOUND — when a SELECT query returns no rows.
    • TOO_MANY_ROWS — when a SELECT returns more than one row.
    • ZERO_DIVIDE — division by zero.
    • VALUE_ERROR — data type conversion error.
  2. User-defined exceptions — declared by the programmer using EXCEPTION and raised with RAISE.

  3. Oracle error exceptions — caught through OTHERS or using SQLCODE and SQLERRM.

Example of exception handling:

DECLARE
  v_num NUMBER;
BEGIN
  SELECT salary INTO v_num FROM employees WHERE employee_id = 9999;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Employee not found');
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Other error: ' || SQLERRM);
END;

Thus, exceptions allow controlling errors and ensuring the reliability of PL/SQL code.