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

C++ Exception Handling का परिचय

Exception handling यह plan करना है कि जब कुछ गलत हो तो आपका program क्या करे, एक spare tire रखने जैसा। यह एक problem को सब कुछ बर्बाद करने से रोकता है।
Syntax
cpp
try {
  // code that might throw
  throw value;
} catch (type variable_name) {
  // handle the error
}

try-catch Block

Exception handling आपको पूरे program को crash किए बिना runtime errors से deal करने का एक structured तरीका देता है: आप fail हो सकने वाले statements को एक try block में wrap करते हैं, और वे जो भी exception throw करें वह एक matching catch block के अंदर catch और handle हो जाता है।

उदाहरण: The try-catch Block

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

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

Exceptions Throw करना

throw keyword यह signal करने का तरीका है कि एक runtime error हुई है; इसके execute होते ही, normal program flow तुरंत रुक जाता है और control सीधे उस nearest catch block पर jump करता है जिसका type थ्रोन चीज़ से match करता है।

उदाहरण: Throwing Exceptions

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

void checkAge(int age) {
	if (age < 0) throw std::string("Invalid age");
}

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		checkAge(-1);
	// Handle the exception if one was thrown
	} catch (std::string &e) {
		// Print to the console with cout
		std::cout << e << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Exception Types Catch करना

आप एक single try block के बाद लगातार कई catch blocks लिख सकते हैं, हर एक एक अलग exception type से match करते हुए। Compiler सिर्फ पहला catch block चलाता है जिसका declared type actually thrown exception से match करे, बाकी skip करते हुए।

उदाहरण: Catching Multiple Exception Types

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

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw 3.14;
	// Handle the exception if one was thrown
	} catch (int e) {
		// Print to the console with cout
		std::cout << "int caught" << std::endl;
	// Handle the exception if one was thrown
	} catch (double e) {
		// Print to the console with cout
		std::cout << "double caught: " << e << std::endl;
	}
	return 0;
}

Standard Exceptions

Standard library stdexcept header में ready-made exception classes देती है, जैसे runtime_error और out_of_range, जिन्हें आप खुद throw कर सकते हैं या उनके common base class, std::exception, के जरिए generically catch कर सकते हैं।

उदाहरण: 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("Something failed");
	// 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;
}

Catch-All Block (...)

एक catch-all block, अपनी parameter list की तरह एक specific type की बजाय तीन dots (...) से लिखा गया, literally किसी भी exception को catch करता है जो पहले के एक ज़्यादा specific catch block से match नहीं हुआ — एक program के outermost level पर एक useful safety net।

उदाहरण: Catch-All Block (...)

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

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw 'x';
	// Handle the exception if one was thrown
	} catch (int e) {
		// Print to the console with cout
		std::cout << "int" << std::endl;
	// Handle the exception if one was thrown
	} catch (...) {
		// Print to the console with cout
		std::cout << "Caught something unexpected" << std::endl;
	}
	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. एक exception को value से catch करना, जो slice करता है।
  2. specific catches से पहले एक general catch(...) रखना।
  3. कुछ भी catch न करना, ताकि program terminate हो जाए।
🔒

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.