Exception Handling in C++ Notes | try, throw, catch, Multiple Catch & Exception Handling Mechanism | Basic Computer Engineering | RGPV BTech First Year
Exception Handling in C++ Notes | try, throw, catch, Multiple Catch & Exception Handling Mechanism | Basic Computer Engineering | RGPV BTech First Year
Exception Handling in C++
Exception Handling C++ Programming का एक अत्यंत महत्वपूर्ण Feature है जिसका उपयोग Program के Execution के दौरान उत्पन्न होने वाली Errors (Exceptions) को Handle करने के लिए किया जाता है। यदि Program में कोई Unexpected Error आ जाए और उसे Handle न किया जाए, तो Program अचानक Terminate हो सकता है। Exception Handling Program को ऐसी परिस्थितियों में भी सुरक्षित रूप से Execute करने में सहायता करती है।
C++ में Exception Handling मुख्यतः try, throw तथा catch Keywords पर आधारित होती है। try Block में Risky Code लिखा जाता है, throw द्वारा Exception Generate किया जाता है तथा catch Block उस Exception को Handle करता है।
RGPV B.Tech First Year के Basic Computer Engineering Subject में Exception Handling in C++ अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Exception, try Block, throw Statement, catch Block एवं Multiple Catch Blocks से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।
↓
try Block
↓
Exception Occurs
↓
throw
↓
catch Block
↓
Program Continues
Introduction to Exception Handling
किसी भी Software Program के Execution के दौरान कई प्रकार की समस्याएँ उत्पन्न हो सकती हैं, जैसे Divide by Zero, Invalid Input, File Not Found, Memory Allocation Failure आदि। यदि इन Errors को उचित तरीके से Handle नहीं किया जाए, तो Program Crash हो सकता है।
Exception Handling ऐसी Errors को Detect तथा Handle करने की तकनीक है, जिससे Program बिना अचानक बंद हुए सुरक्षित रूप से अपना Execution जारी रख सकता है।
What is an Exception?
Exception वह असामान्य (Abnormal) स्थिति होती है जो Program के सामान्य Execution Flow को बाधित (Interrupt) कर देती है। Exception Runtime के दौरान उत्पन्न होती है तथा यदि इसे Handle न किया जाए, तो Program Terminate हो सकता है।
ऊपर दिए गए सभी उदाहरण Runtime Exceptions को दर्शाते हैं जिन्हें Exception Handling द्वारा Handle किया जा सकता है।
What is Exception Handling?
Exception Handling वह प्रक्रिया है जिसमें Program के दौरान उत्पन्न होने वाली Exceptions को Detect करके उचित Action लिया जाता है ताकि Program सुरक्षित रूप से आगे Execute हो सके।
C++ में Exception Handling के लिए मुख्य रूप से try, throw तथा catch का उपयोग किया जाता है।
Need of Exception Handling
Modern Software Development में Program को Robust तथा Reliable बनाना आवश्यक होता है। Exception Handling Program को Unexpected Situations में भी सुरक्षित रूप से Execute करने में सहायता करती है।
- Program Crash होने से बचाने के लिए।
- Runtime Errors को Handle करने के लिए।
- User Friendly Error Messages Display करने के लिए।
- Program की Reliability बढ़ाने के लिए।
- Large Software Applications में Stable Execution सुनिश्चित करने के लिए।
- Resource Leakage रोकने के लिए।
- Professional Software Development के लिए।
Advantages of Exception Handling
| Advantage | Description |
|---|---|
| Error Handling | Runtime Errors को सुरक्षित रूप से Handle करता है। |
| Program Stability | Program को अचानक बंद होने से बचाता है। |
| Reliable Software | Software की Reliability बढ़ाता है। |
| Easy Debugging | Errors को पहचानना एवं सुधारना आसान हो जाता है। |
| Better User Experience | Meaningful Error Messages द्वारा बेहतर User Experience प्रदान करता है। |
Characteristics of Exception Handling
Exception Handling की कुछ महत्वपूर्ण विशेषताएँ (Characteristics) होती हैं जो Program को Runtime Errors से सुरक्षित बनाती हैं। इसकी सहायता से Program Error आने पर भी अचानक बंद नहीं होता तथा उचित तरीके से Execution जारी रख सकता है।
- Runtime Errors को Handle करने की सुविधा प्रदान करता है।
- Program के Normal Execution Flow को सुरक्षित रखता है।
- try, throw तथा catch Keywords पर आधारित होता है।
- Program की Reliability एवं Stability बढ़ाता है।
- Error आने पर उचित Message Display किया जा सकता है।
- Complex Applications में Error Management को सरल बनाता है।
- Program Crash होने की संभावना कम हो जाती है।
Exception Handling Mechanism
C++ में Exception Handling तीन मुख्य Components द्वारा कार्य करती है। सबसे पहले Risky Code try Block में लिखा जाता है। यदि कोई Error आती है, तो throw द्वारा Exception Generate किया जाता है तथा संबंधित catch Block उसे Handle करता है।
↓
Exception Generated
↓
throw Statement
↓
Matching catch Block
↓
Exception Handled
try Block
try Block वह Block होता है जिसमें ऐसा Code लिखा जाता है जहाँ Runtime Error आने की संभावना होती है। यदि इस Block में कोई Exception Generate होती है, तो Program सीधे संबंधित catch Block पर Control Transfer कर देता है।
Program में एक या एक से अधिक try Blocks हो सकते हैं, लेकिन प्रत्येक try Block के साथ कम से कम एक catch Block होना आवश्यक होता है।
throw Statement
throw Keyword का उपयोग Exception Generate करने के लिए किया जाता है। जब Program किसी Error Condition को Detect करता है, तब throw Statement द्वारा संबंधित Exception को Generate किया जाता है।
ऊपर दिए गए उदाहरण में String Type की Exception Generate की गई है जिसे बाद में संबंधित catch Block द्वारा Handle किया जाएगा।
catch Block
catch Block Exception को Handle करने के लिए उपयोग किया जाता है। यदि throw द्वारा Generate की गई Exception का Type catch Block से Match करता है, तो वही Block Execute होता है।
catch Block में Error Message Display किया जा सकता है अथवा Program के लिए आवश्यक Recovery Process की जा सकती है।
Basic Exception Handling Program
Output
इस उदाहरण में throw 100; द्वारा Integer Exception Generate की गई है जिसे catch(int x) Block ने Successfully Handle किया है।
Multiple Catch Blocks
कई बार Program में विभिन्न प्रकार (Different Types) की Exceptions उत्पन्न हो सकती हैं। ऐसी स्थिति में C++ एक ही try Block के साथ एक से अधिक catch Blocks लिखने की सुविधा प्रदान करता है। इसे Multiple Catch Blocks कहा जाता है।
Exception जिस Type की होती है, उसी Type का catch Block Execute होता है जबकि अन्य Catch Blocks Skip कर दिए जाते हैं।
ऊपर दिए गए उदाहरण में Integer Exception Generate हुई है, इसलिए केवल पहला catch(int) Block Execute होगा।
Advantages of Multiple Catch Blocks
- अलग-अलग प्रकार की Exceptions को अलग-अलग Handle किया जा सकता है।
- Error Handling अधिक व्यवस्थित (Organized) हो जाती है।
- Program की Readability बढ़ती है।
- Specific Error के लिए अलग Recovery Process लिखी जा सकती है।
- Large Software Applications में Error Management सरल हो जाता है।
Nested try-catch Block
जब किसी try Block के अंदर एक और try-catch Block लिखा जाता है, तो उसे Nested try-catch कहा जाता है। इसका उपयोग Complex Error Handling के लिए किया जाता है।
↓
Inner try
↓
Exception
↓
Inner catch
↓
Outer catch (यदि आवश्यक हो)
Nested try-catch द्वारा Program में विभिन्न स्तरों (Levels) पर Exceptions को Handle किया जा सकता है।
Example of Nested try-catch
इस उदाहरण में Exception पहले Inner Catch द्वारा Handle हो जाती है, इसलिए Outer Catch Execute नहीं होता।
Rethrowing an Exception
कभी-कभी किसी Exception को पहले Handle करने के बाद उसे पुनः (Again) दूसरे Level पर भेजने की आवश्यकता होती है। इसे Rethrowing an Exception कहा जाता है।
ऊपर दिए गए उदाहरण में Exception को Handle करने के बाद पुनः throw; द्वारा अगले Appropriate Catch Block की ओर भेजा गया है।
Benefits of Nested and Multiple Exception Handling
| Feature | Benefit |
|---|---|
| Multiple Catch | Different Types की Exceptions अलग-अलग Handle होती हैं। |
| Nested try-catch | Multi-Level Error Handling संभव होती है। |
| Rethrowing | Exception को Higher Level पर Handle किया जा सकता है। |
| Better Reliability | Program अधिक Stable एवं Secure बनता है। |
| Maintainability | Complex Programs को Maintain करना आसान होता है। |
Standard Exceptions in C++
C++ Standard Library में कई Predefined Exception Classes उपलब्ध होती हैं जिन्हें Standard Exceptions कहा जाता है। ये Exceptions Program में होने वाली सामान्य Runtime Errors को Handle करने के लिए उपयोग की जाती हैं। Standard Exceptions <exception> Header File में उपलब्ध होती हैं।
| Standard Exception | Purpose |
|---|---|
| bad_alloc | Memory Allocation Failure को दर्शाता है। |
| bad_cast | Invalid Type Casting होने पर Generate होता है। |
| bad_typeid | Invalid Type Information Access होने पर। |
| logic_error | Program Logic से संबंधित Errors। |
| runtime_error | Runtime के दौरान होने वाली Errors। |
User Defined Exceptions
जब Program की आवश्यकता के अनुसार स्वयं की Exception Create की जाती है, तो उसे User Defined Exception कहा जाता है। Programmer अपनी आवश्यकता अनुसार किसी भी Data Type अथवा Class Object को Exception के रूप में throw कर सकता है।
ऊपर दिए गए उदाहरण में InvalidAge नाम की User Defined Exception Class बनाई गई है।
Example of User Defined Exception
Output
ऊपर दिए गए उदाहरण में यदि Age 18 से कम होती है, तो InvalidAge नाम की User Defined Exception Generate होती है।
Stack Unwinding
जब किसी Function में Exception Generate होती है और उसी Function में उसका उचित catch Block उपलब्ध नहीं होता, तब Program उस Function को छोड़कर Calling Function में लौट जाता है। इस प्रक्रिया को Stack Unwinding कहा जाता है।
↓
Function B
↓
Function C
↓
Exception
↓
Stack Unwinding
↓
Matching Catch Block
Stack Unwinding के दौरान सभी Local Objects के Destructors Automatically Execute होते हैं, जिससे Resources Proper तरीके से Release हो जाते हैं।
Advantages of Standard and User Defined Exceptions
- Different Types की Errors को अलग-अलग Handle किया जा सकता है।
- Program अधिक Secure एवं Reliable बनता है।
- Custom Business Rules के अनुसार Exceptions बनाई जा सकती हैं।
- Large Software Projects में Error Handling सरल हो जाती है।
- Code की Maintainability एवं Reusability बढ़ती है।
- Professional Software Development में Structured Error Handling संभव होती है।
Applications of Exception Handling
Exception Handling का उपयोग लगभग सभी Professional Software Applications में किया जाता है जहाँ Runtime Errors आने की संभावना होती है। इसकी सहायता से Program Unexpected Situations में भी सुरक्षित रूप से कार्य करता रहता है।
| Application | Purpose |
|---|---|
| Banking Software | Invalid Transaction तथा Insufficient Balance Errors Handle करना। |
| ATM System | Wrong PIN एवं Invalid Operations Handle करना। |
| File Handling | File Not Found तथा Permission Errors Handle करना। |
| Database Applications | Database Connection एवं Query Errors Handle करना। |
| Network Applications | Connection Failure तथा Timeout Errors Handle करना। |
| E-Commerce Applications | Payment तथा Order Processing Errors Handle करना। |
Advantages of Exception Handling
- Runtime Errors को व्यवस्थित तरीके से Handle किया जा सकता है।
- Program अचानक Terminate नहीं होता।
- Program की Reliability एवं Stability बढ़ती है।
- Meaningful Error Messages User को दिखाए जा सकते हैं।
- Code अधिक Readable तथा Maintainable बनता है।
- Error Handling तथा Normal Logic अलग-अलग लिखे जा सकते हैं।
- Large Software Projects में Debugging आसान हो जाती है।
- Resources को Proper तरीके से Release किया जा सकता है।
Limitations of Exception Handling
- अत्यधिक Exception Handling Program को Complex बना सकती है।
- Improper Exception Design से Performance प्रभावित हो सकती है।
- हर Error को Exception द्वारा Handle करना उचित नहीं होता।
- गलत Catch Block लिखने पर Exception Handle नहीं होगी।
- Exception Flow समझना नए Programmers के लिए कठिन हो सकता है।
- Uncaught Exception Program को Terminate कर सकती है।
Best Practices for Exception Handling
- केवल Exceptional Situations के लिए Exception का उपयोग करें।
- Specific Exception Types को Catch करें।
- Meaningful Error Messages प्रदर्शित करें।
- Resources Release करने के बाद ही Program Continue करें।
- Nested try-catch का उपयोग केवल आवश्यकता होने पर करें।
- Custom Exceptions का उपयोग Business Rules के अनुसार करें।
- Exception Handling तथा Business Logic को अलग रखें।
Exception Handling Flow Chart
↓
try Block
↓
Exception?
↓
Yes
↓
throw
↓
Matching catch
↓
Exception Handled
↓
Program Continues
Real Life Example
मान लीजिए किसी Online Banking System में User Amount Withdraw करता है। यदि Account में पर्याप्त Balance नहीं है, तो Program Insufficient Balance Exception Generate कर सकता है। यह Exception catch Block द्वारा Handle की जाती है और User को उचित Error Message दिखाया जाता है, जबकि Program सुरक्षित रूप से आगे कार्य करता रहता है।
Summary
Exception Handling C++ का एक महत्वपूर्ण Error Handling Mechanism है जिसका उपयोग Runtime Errors को सुरक्षित रूप से Handle करने के लिए किया जाता है। C++ में Exception Handling मुख्य रूप से try, throw तथा catch Keywords पर आधारित होती है। इसकी सहायता से Program Unexpected Errors आने पर भी बिना Crash हुए अपना Execution जारी रख सकता है।
| Concept | Description |
|---|---|
| Exception | Runtime के दौरान उत्पन्न होने वाली असामान्य (Abnormal) स्थिति। |
| Exception Handling | Runtime Errors को Detect एवं Handle करने की प्रक्रिया। |
| try Block | Risky Code लिखने के लिए उपयोग किया जाता है। |
| throw Statement | Exception Generate करने के लिए उपयोग किया जाता है। |
| catch Block | Generated Exception को Handle करता है। |
| Multiple Catch | Different Types की Exceptions को अलग-अलग Handle करता है। |
| Stack Unwinding | Matching Catch मिलने तक Function Calls वापस लौटते हैं। |
| User Defined Exception | Programmer द्वारा बनाई गई Custom Exception। |
Important Viva Questions
- Exception क्या है?
- Exception Handling क्या है?
- try Block का क्या कार्य है?
- throw Statement का उपयोग क्यों किया जाता है?
- catch Block क्या है?
- Multiple Catch Blocks क्या होते हैं?
- Nested try-catch क्या है?
- Stack Unwinding क्या है?
- Standard Exception तथा User Defined Exception में क्या अंतर है?
- Exception Handling के मुख्य Advantages क्या हैं?
University Examination Important Questions
- Exception क्या है? Exception Handling की आवश्यकता समझाइए।
- Exception Handling Mechanism को Diagram सहित समझाइए।
- try, throw तथा catch Keywords को उदाहरण सहित समझाइए।
- Multiple Catch Blocks को उदाहरण सहित समझाइए।
- Nested try-catch क्या है? उदाहरण सहित समझाइए।
- Rethrowing Exception क्या है? इसकी आवश्यकता लिखिए।
- Standard Exception एवं User Defined Exception में अंतर लिखिए।
- Stack Unwinding क्या है? इसकी कार्यप्रणाली समझाइए।
- Exception Handling के Advantages एवं Limitations लिखिए।
- C++ में Exception Handling का Complete Program उदाहरण सहित लिखिए।
Exam Tips
- हमेशा याद रखें कि try Block में Risky Code लिखा जाता है।
- throw Statement Exception Generate करता है।
- catch Block Exception को Handle करता है।
- एक try Block के साथ Multiple Catch Blocks लिखे जा सकते हैं।
- Matching Data Type का Catch Block ही Execute होता है।
- Stack Unwinding तथा User Defined Exception परीक्षा के महत्वपूर्ण Topics हैं।
- RGPV Exam में try-throw-catch का Program अक्सर पूछा जाता है।
Conclusion
Exception Handling in C++ आधुनिक Object Oriented Programming का एक अत्यंत महत्वपूर्ण Feature है जो Runtime Errors को सुरक्षित एवं व्यवस्थित तरीके से Handle करने की सुविधा प्रदान करता है। try, throw तथा catch Keywords की सहायता से Program Unexpected Situations में भी बिना Crash हुए अपना Execution जारी रख सकता है। Multiple Catch Blocks, Nested try-catch, Stack Unwinding तथा User Defined Exceptions Complex Software Development में Error Management को अधिक प्रभावी बनाते हैं। Banking Software, Database Applications, File Handling, Network Programming तथा Enterprise Systems में Exception Handling का व्यापक उपयोग किया जाता है। RGPV B.Tech First Year के विद्यार्थियों के लिए यह Topic Theory, Practical एवं Viva तीनों दृष्टिकोण से अत्यंत महत्वपूर्ण है।
Related Articles
Arrays of Objects in C++ Notes | Object Array, Declaration, Initialization & Examples | Basic Computer Engineering | RGPV BTech First Year
Arrays ...
Read More →Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year
Object ...
Read More →Pointers to Objects in C++ Notes | Object Pointer, Accessing Members using Pointer & Examples | Basic Computer Engineering | RGPV BTech First Year
Pointer...
Read More →Dynamic Memory Allocation in C++ Notes | new & delete Operators, Heap Memory, Dynamic Objects & Examples | Basic Computer Engineering | RGPV BTech First Year
Dynamic...
Read More →this Pointer in C++ Notes | this Keyword, Uses, Advantages & Examples | Basic Computer Engineering | RGPV BTech First Year
this Po...
Read More →