0% found this document useful (0 votes)
3 views

PLSQL_Exception_Handling_Interview_Questions

Uploaded by

sachinnayaka999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PLSQL_Exception_Handling_Interview_Questions

Uploaded by

sachinnayaka999
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PL/SQL Exception Handling Interview Questions & Answers

1. Handling NO_DATA_FOUND Exception


----------------------------------
DECLARE
v_emp_name employees.first_name%TYPE;
BEGIN
SELECT first_name INTO v_emp_name FROM employees WHERE employee_id = 9999;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END;
/

2. Handling TOO_MANY_ROWS Exception


-----------------------------------
DECLARE
v_emp_name employees.first_name%TYPE;
BEGIN
SELECT first_name INTO v_emp_name FROM employees WHERE department_id = 10;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Multiple employees found, use a cursor instead.');
END;
/

3. Handling Division by Zero Exception


--------------------------------------
DECLARE
v_result NUMBER;
BEGIN
v_result := 100 / 0;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Division by zero error occurred.');
END;
/

4. User-Defined Exception for Low Salary


----------------------------------------
DECLARE
v_salary employees.salary%TYPE := 4000;
low_salary EXCEPTION;
BEGIN
IF v_salary < 5000 THEN
RAISE low_salary;
END IF;
EXCEPTION
WHEN low_salary THEN
DBMS_OUTPUT.PUT_LINE('Error: Salary must be at least 5000.');
END;
/

5. Handling Unknown Exceptions and Logging Errors


-------------------------------------------------
DECLARE
v_emp_id NUMBER := 9999;
BEGIN
SELECT first_name INTO v_emp_name FROM employees WHERE employee_id = v_emp_id;
EXCEPTION
WHEN OTHERS THEN
INSERT INTO error_log (error_code, error_message, error_time)
VALUES (SQLCODE, SQLERRM, SYSDATE);
COMMIT;
DBMS_OUTPUT.PUT_LINE('An error occurred. Logged into error_log table.');
END;
/

6. Using RAISE_APPLICATION_ERROR
--------------------------------
DECLARE
v_salary employees.salary%TYPE := 3000;
BEGIN
IF v_salary < 5000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too low! Must be at least 5000.');
END IF;
END;
/

7. Raising Exception for Invalid Department


-------------------------------------------
DECLARE
v_dept_id NUMBER := 9999;
invalid_department EXCEPTION;
BEGIN
SELECT department_id INTO v_dept_id FROM departments WHERE department_id = v_dept_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE invalid_department;
WHEN invalid_department THEN
DBMS_OUTPUT.PUT_LINE('Error: Invalid department ID.');
END;
/

8. Using PRAGMA EXCEPTION_INIT


------------------------------
DECLARE
table_not_found EXCEPTION;
PRAGMA EXCEPTION_INIT(table_not_found, -942);
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM non_existing_table';
EXCEPTION
WHEN table_not_found THEN
DBMS_OUTPUT.PUT_LINE('Error: Table does not exist.');
END;
/

9. Using SQLCODE and SQLERRM for Error Messages


----------------------------------------------
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM non_existing_table';
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error Code: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('Error Message: ' || SQLERRM);
END;
/

10. Retrying an Operation on Exception


--------------------------------------
DECLARE
v_count NUMBER := 0;
retry_limit CONSTANT NUMBER := 3;
BEGIN
FOR i IN 1..retry_limit LOOP
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM non_existing_table';
EXIT;
EXCEPTION
WHEN OTHERS THEN
v_count := v_count + 1;
DBMS_OUTPUT.PUT_LINE('Retrying: Attempt ' || v_count);
IF v_count = retry_limit THEN
DBMS_OUTPUT.PUT_LINE('Operation failed after ' || retry_limit || ' attempts.');
END IF;
END;
END LOOP;
END;
/

You might also like