Exception Handling in OOP
Understanding error handling and exception management in C++
What is Exception Handling?
Exception handling is a mechanism that allows a program to handle runtime errors gracefully. It provides a way to transfer control from one part of a program to another when an exceptional condition occurs.
Exception Handling क्या है? (हिंदी में)
Exception handling एक ऐसा mechanism है जो program को runtime errors को gracefully handle करने की सुविधा देता है। यह program के एक part से दूसरे part में control transfer करने का तरीका provide करता है जब कोई exceptional condition occur होती है।
Key Points:
- Error Handling: Manages runtime errors gracefully
- Control Flow: Transfers control when exceptions occur
- Resource Management: Ensures proper cleanup of resources
- Error Information: Provides detailed error information
Types of Exceptions
Standard Exceptions
Built-in exceptions like std::runtime_error
Custom Exceptions
User-defined exception classes
System Exceptions
OS-level exceptions and signals
Exception Handling Rules
Rule | Description |
---|---|
Try Block | Code that might throw exceptions |
Catch Block | Handles specific exceptions |
Throw | Raises an exception |
Finally | Cleanup code (not in C++) |
Simple Example
#include <iostream> #include <stdexcept> #include <string> // Custom exception class class DivisionByZero : public std::runtime_error { public: DivisionByZero() : std::runtime_error("Division by zero") {} }; // Function that might throw exceptions double divide(double a, double b) { if (b == 0) { throw DivisionByZero(); } return a / b; } // Function with multiple exception handling void processNumbers() { try { // Try block with multiple operations double result1 = divide(10, 2); std::cout << "10 / 2 = " << result1 << std::endl; double result2 = divide(10, 0); // This will throw std::cout << "10 / 0 = " << result2 << std::endl; } catch (const DivisionByZero& e) { std::cerr << "Error: " << e.what() << std::endl; } catch (const std::exception& e) { std::cerr << "Standard exception: " << e.what() << std::endl; } catch (...) { std::cerr << "Unknown exception occurred" << std::endl; } } // Function demonstrating exception propagation void outerFunction() { try { processNumbers(); } catch (const std::exception& e) { std::cerr << "Caught in outer function: " << e.what() << std::endl; } } int main() { // Demonstrating exception handling std::cout << "Testing exception handling:" << std::endl; outerFunction(); // Demonstrating resource management try { // Resource acquisition std::string* resource = new std::string("Resource"); // Simulating an error throw std::runtime_error("Resource error"); // This won't execute delete resource; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; // Resource cleanup should be handled by RAII } return 0; }
उदाहरण की व्याख्या (Example Explanation)
इस उदाहरण में exception handling के different aspects को demonstrate किया गया है:
- Custom Exception: DivisionByZero class एक custom exception है
- Try-Catch Blocks: Multiple catch blocks different types के exceptions को handle करते हैं
- Exception Propagation: Exceptions outer functions तक propagate होते हैं
- Resource Management: RAII principle का use resource cleanup के लिए किया गया है
Benefits of Exception Handling
Exception Handling के फायदे
-
Error Management
- Errors को gracefully handle करने में help करता है
- Program crashes को prevent करता है
-
Code Organization
- Error handling code को main logic से separate करता है
- Code readability को improve करता है
-
Resource Management
- Resources को properly cleanup करने में help करता है
- Memory leaks को prevent करता है
Best Practices
- Use RAII for resource management
- Throw by value, catch by reference
- Keep exception handling code separate
- Use specific exception types
- Avoid throwing exceptions in destructors