← Back to C++ Course | Chapter 12: Exception Handling | Lesson 5 of 5

C++ में Exception Hierarchy

C++ के standard exceptions एक family tree में organize हैं, general problems parents की तरह और specific ones children की तरह। इसे जानना सही kind catch करने में मदद करता है।
Syntax
cpp
#include <stdexcept>

try {
  throw std::runtime_error("message");
} catch (const std::exception &e) {
  e.what();
}

Standard Exceptions का Overview

C++ के standard exceptions std::exception पर rooted एक hierarchy बनाते हैं, जो दो broad categories में branch होता है: unpredictable external factors से cause हुई problems के लिए std::runtime_error, और एक careful programmer जिन्हें पहले से रोक सकता था उन bugs के लिए std::logic_error।

उदाहरण: Overview of Standard Exceptions

cpp
// Include std::cout and std::cin
#include <iostream>
#include <stdexcept>

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw std::runtime_error("Runtime issue");
	// Handle the exception if one was thrown
	} catch (std::exception &e) {
		// Print to the console with cout
		std::cout << e.what() << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Runtime Errors (runtime_error)

एक runtime_error एक ऐसी failure represent करता है जिसे अकेले logic checks reliably पहले से predict नहीं कर सकते — जैसे एक network timeout, एक hardware fault, या एक full disk, जो program के अपने control से बाहर की conditions पर depend करते हैं।

उदाहरण: Runtime Errors (runtime_error)

cpp
// Include std::cout and std::cin
#include <iostream>
#include <stdexcept>

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw std::runtime_error("Network timeout");
	// Handle the exception if one was thrown
	} catch (std::runtime_error &e) {
		// Print to the console with cout
		std::cout << e.what() << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logic Errors (logic_error)

इसके contrast में एक logic_error, एक ऐसी mistake represent करता है जो correct code से theoretically avoidable थी — एक invalid argument pass करना, या एक container element access करने की कोशिश करना जो range से बाहर है, दोनों preventable logic errors के classic examples हैं।

उदाहरण: Logic Errors (logic_error)

cpp
// Include std::cout and std::cin
#include <iostream>
#include <stdexcept>

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw std::invalid_argument("Invalid argument passed");
	// Handle the exception if one was thrown
	} catch (std::logic_error &e) {
		// Print to the console with cout
		std::cout << e.what() << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base Reference से Catch करना

आपको हमेशा exceptions को value की बजाय const reference से catch करना चाहिए, क्योंकि reference से catch करना object का real polymorphic type preserve करता है और object slicing avoid करता है, जो अन्यथा एक derived exception को सिर्फ इसके base class हिस्से तक काट देगा।

उदाहरण: Catching by Base Reference

cpp
// Include std::cout and std::cin
#include <iostream>
#include <stdexcept>

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw std::out_of_range("Index out of range");
	// Handle the exception if one was thrown
	} catch (const std::exception &e) {
		// Print to the console with cout
		std::cout << e.what() << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bad Allocations (bad_alloc)

std::bad_alloc सीधे standard library के memory allocator से throw होता है — सबसे common new operator से triggered — जब भी system genuinely free heap memory खत्म कर देता है और एक allocation request बस satisfy नहीं हो सकती।

उदाहरण: Bad Allocations (bad_alloc)

cpp
// Include std::cout and std::cin
#include <iostream>
#include <new>

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw std::bad_alloc();
	// Handle the exception if one was thrown
	} catch (std::bad_alloc &e) {
		// Print to the console with cout
		std::cout << e.what() << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक derived से पहले एक base exception catch करना, ताकि derived handler कभी न चले।
  2. reference की बजाय value से catch करना, जो exception slice करता है।
  3. runtime_error या logic_error इस्तेमाल करने से पहले #include <stdexcept> भूल जाना।
चैप्टर सारांश
  • Exception handling program crash किए बिना runtime errors से deal करता है।
  • try, catch, और throw exceptions signal और handle करते हैं।
  • Custom exceptions और एक exception hierarchy आपको meaningful error types बनाने देते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.