Exception Handling in PL/SQL: Named System, Unnamed System, User-defined Exceptions

एक exception एक runtime error है जो आपके code को पटरी से उतारता है; इसे handle करना एक electrical fuse box लगाने जैसा है ताकि एक अकेला short-circuit आपकी पूरी application को जला न दे।

12 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

टूटा Exam Counter

कल्पना कीजिए आप एक college counter पर student hall tickets process करते एक administrator हैं। सब कुछ सहजता से चलता है जब तक एक missing fee status वाला student आगे नहीं आता। अगर आप घबरा जाते हैं, freeze हो जाते हैं, और पूरी registration window बंद कर देते हैं, उनके पीछे line में इंतज़ार करते सैकड़ों दूसरे students भुगतेंगे। programming में, runtime errors, जैसे शून्य से भाग देना, एक null value pass करना, या एक ऐसा student record खोजना जो delete हो गया है, पूरी तरह अपरिहार्य हैं। अगर आपका program नहीं जानता कैसे react करे, यह घबराता है, database session crash करता है, और user को disconnect करता है। Exception handling हमारा system को यह बताने का तरीक़ा है: 'अगर एक ख़ास error आपको गिराए, पूरी application crash मत करो; issue को एक emergency handler की ओर मोड़ो और बाक़ी सबके लिए normal operations चलाते रहो!'

Theory

घर का Fuse Box

एक exception handler को एक Indian घर में लगे एक electrical fuse box या एक MCB (Miniature Circuit Breaker) की तरह सोचिए। जब एक किचन appliance में एक भारी voltage surge या एक short circuit होता है, fuse सुरक्षित रूप से उड़ जाता है ताकि केवल उस क्षतिग्रस्त line को current काटे, आपके महँगे TVs और refrigerators को जलने से बचाते हुए। PL/SQL में, EXCEPTION section बिल्कुल वह safety fuse है। यह high-voltage code failures पकड़ता है, उन्हें साफ़-सुथरे neutralize करता है, और आस-पास के main database server को स्थिर और चलता रखता है।

Theory

Runtime Control के तीन बचाव

एक exception execution के दौरान उठा एक database warning या error condition है जो regular instruction flow में बाधा डालता है। PL/SQL इन disruptions को इस आधार पर तीन अलग categories में साफ़-सुथरे classify करता है कि किसने problem खोजी और यह system memory के अंदर कैसे flag होती है।

At a glance

PL/SQL anomaly management layers का संरचनात्मक वर्गीकरण।

Exception Categoryइसे कौन परिभाषित करता है?Triggering MechanismIdentification Method
1. Named System ExceptionOracle Enginestandard database rules के उल्लंघन पर अपने-आप उठता है।Oracle द्वारा NO_DATA_FOUND या ZERO_DIVIDE जैसे आम descriptive words पर pre-mapped।
2. Unnamed System ExceptionOracle Enginegeneric, दुर्लभ system error codes के लिए अपने-आप उठता है।एक compiler directive इस्तेमाल करके map होने तक केवल एक raw numeric code (जैसे ORA-02292) से पहचाना जाता है।
3. User-Defined ExceptionApplication Programmercustom business logic टूटने पर developer द्वारा हाथ से invoke।एक EXCEPTION variable type के रूप में declared और executable code के अंदर RAISE इस्तेमाल करके स्पष्ट रूप से जलाया गया।

Practical

पूर्ण College Registration और Fee Auditor

-- An anonymous block showcasing the operational differences across all three exception types
DECLARE
  -- A. Declaring a User-Defined exception for institutional rules
  e_low_attendance EXCEPTION;
  
  -- B. Naming an Unnamed System Exception for foreign key check failures (ORA-02292)
  e_child_record_found EXCEPTION;
  PRAGMA EXCEPTION_INIT(e_child_record_found, -2292);
  
  v_name       students.student_name%TYPE;
  v_attendance NUMBER := 68; -- Below university requirement
BEGIN
  -- 1. Simulating a Named System Exception trap
  -- If ID 99999 doesn't exist, this statement instantly triggers NO_DATA_FOUND
  SELECT student_name INTO v_name FROM students WHERE student_id = 99999;
  
  -- 2. Simulating a User-Defined Exception ignition
  IF v_attendance < 75 THEN
    RAISE e_low_attendance;
  END IF;
  
EXCEPTION
  -- Handling Named System Errors
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Catch 1: That student ID was not found in the university registry.');
    
  -- Handling Custom Business Errors
  WHEN e_low_attendance THEN
    DBMS_OUTPUT.PUT_LINE('Catch 2: Admit Card Denied! Attendance is lower than the mandatory 75%.');
    
  -- Handling Unnamed System Errors mapped by hand
  WHEN e_child_record_found THEN
    DBMS_OUTPUT.PUT_LINE('Catch 3: Integrity Error! Cannot wipe course because active students are tied to it.');
    
  -- Global safety catch-all trap
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Catch 4: An unhandled anomaly occurred. System Code: ' || SQLCODE);
END;
/

Copy करके Oracle FreeSQL खोलें
Oracle FreeSQL, Oracle SQL का मुफ़्त online editor है। Code पहले copy हो जाता है: उसे वहां paste करके Run करें।

Follow along

एक User-Defined Exception का Lifecycle

  1. 1. Blueprint (DECLARE) developer local block के अंदर एक नया custom exception identifier नाम register करता है, compiler को इसे एक unique error signature के रूप में track करने की सूचना देते हुए।
  2. 2. Rule Check (IF) operational program custom domain requirements का उल्लंघन करती conditions पकड़ने के लिए business logic variables जाँचता है।
  3. 3. Spark (RAISE) code एक उल्लंघन से टकराता है और स्पष्ट रूप से exception के नाम के बाद RAISE command जारी करता है, execution sequence तुरंत रोकते हुए।
  4. 4. Interception (WHEN) execution pointer code की बची lines छोड़ता है, तुरंत EXCEPTION block में गिरता है, error token मिलाता है, और safety code पूरा करता है।

Quiz

कौन सा PL/SQL programmatic instruction एक non-predefined numeric Oracle server error code (जैसे ORA-02292) को एक developer के custom named exception token से bind करता है?

  1. RAISE_APPLICATION_ERROR
  2. PRAGMA EXCEPTION_INIT
  3. WHEN OTHERS THEN
  4. ALTER EXCEPTION TYPE
Show the answer

PRAGMA EXCEPTION_INIT

compile-time directive PRAGMA EXCEPTION_INIT स्पष्ट रूप से compiler को एक custom named exception variable को एक ख़ास negative Oracle database error number से bind करने का निर्देश देता है।

Watch out

ख़तरनाक Silent Killer: WHEN OTHERS THEN NULL

lab examinations के लिए ध्यान दीजिए! कई students की आदत होती है error screens bypass करने के लिए अपने blocks के अंत में बस WHEN OTHERS THEN NULL; लिखना। यह एक अत्यधिक ख़तरनाक असली दुनिया की आदत है। THEN NULL लिखना एक programming black hole बनाता है, यह हर अप्रत्याशित error, data corruption, या memory failure को बिना log किए चुपचाप निगल जाता है। user सोचता है उनकी कार्रवाई सफल हुई, जबकि disk पर data टूटा है, debugging को पूरी तरह असंभव बनाते हुए!

Think first

One-Way Execution Trap जाँच

अगर एक error एक मेल खाते exception block द्वारा रोका और सँभाला जाता है, क्या execution pointer main BEGIN section के अंदर बची lines पूरी करने के लिए वापस ऊपर कूदता है?

Show the answer

नहीं, यह कभी नहीं लौटता! एक बार control EXCEPTION block में transfer होता है, यह primary block layout से बाहर एक one-way trip है। एक बार exception handling instructions execute होते हैं, पूरा PL/SQL block समाप्त होता है। अगर आपको एक अलग failure के बावजूद बची operations जारी रखनी हैं, आपको उस ख़ास high-risk statement को इसके अपने समर्पित nested BEGIN-END sub-block के अंदर encapsulate करना होगा।

Theory

External Viva Scoring का राज़

lab exams के दौरान external university evaluators का सामना करते समय, वे पूछना पसंद करते हैं: 'एक WHEN OTHERS handle के अंदर कौन से built-in functions error details पढ़ सकते हैं?' उन्हें तुरंत प्रभावित कीजिए `SQLCODE` (जो सक्रिय negative error number return करता है) और `SQLERRM` (जो error message की असल descriptive text व्याख्या return करता है) का नाम लेकर।

Summary

Key takeaways

  • Exceptions runtime error anomalies हैं जो standard program paths को चलने से रोकती हैं।
  • Named system errors (जैसे NO_DATA_FOUND) internal Oracle engine द्वारा pre-named हैं।
  • Unnamed system errors के पास codes हैं पर नाम नहीं; उन्हें label करने के लिए PRAGMA EXCEPTION_INIT इस्तेमाल कीजिए।
  • User-defined exceptions custom application rules लागू करती हैं और RAISE से स्पष्ट रूप से trigger होनी होंगी।
  • एक बार एक error fire होता है, execution exception section में नीचे कूदता है और कभी वापस ऊपर नहीं लौटता।
  • अप्रत्याशित bugs पकड़ने के लिए एक generic WHEN OTHERS catch block के भीतर SQLCODE और SQLERRM इस्तेमाल कीजिए।
  • Memory hook: custom नाम declare करो, अपने rules जाँचो, टूटने पर RAISE का झंडा उठाओ, और इसे WHEN statement पर पकड़ो!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Cursors and Exception Handling

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Exception Handling in PL/SQL: Named System, Unnamed System, User-defined Exceptions · Concepts of Relational Database Management Systems · Gri-Learn