Exception Handling
Exception Handling: free step-by-step lesson with examples, common mistakes, and interview tips — part of Oracle SQL Tutorial on Toolliyo Academy.
On this page
Oracle SQL Tutorial · Lesson 62 of 96
Exception Handling
Setup & Connect ✓ → Architecture & SQL ✓ → DBA & Tuning → Projects
DBA & Tuning · 3 — Production · ~10 min · Oracle — SQL & PL/SQL
What is this?
PL/SQL exception handling catches errors like NO_DATA_FOUND or ZERO_DIVIDE so your program can respond instead of crashing the session.
Why should you care?
Batch jobs in telecom billing must log bad rows and continue — not stop the whole night run.
See it live — copy this example
Run these statements in SQL Developer or SQL*Plus against your lab PDB. Prefer a practice user — not SYS — for application SQL.
DECLARE
v_bal NUMBER;
v_id NUMBER := 999;
BEGIN
SELECT balance INTO v_bal FROM customers WHERE customer_id = v_id;
DBMS_OUTPUT.PUT_LINE('Balance=' || v_bal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer ' || v_id || ' not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
RAISE;
END;
/
What happened?
- SELECT INTO expects exactly one row.
- NO_DATA_FOUND runs if zero rows.
- WHEN OTHERS is a safety net; RAISE re-throws after logging.
Practice next
- SET SERVEROUTPUT ON in SQL*Plus/SQL Developer.
- Run the block with a missing id.
- Change v_id to an existing customer.
- Raise a custom error with RAISE_APPLICATION_ERROR.
- Write the error into an error_log table.
Remember
Catch expected errors explicitly. Log SQLERRM for support. Do not hide failures in empty handlers.
Nightly billing batch
One bad account must not stop 2 million invoice rows.
Outcome: Handler logs the bad id and continues.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!