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

C++ में try-catch

try-catch एक safety net जैसा है: आप कुछ risky try करते हैं और, अगर यह fail हो, problem catch करते हैं और calmly respond करते हैं। Program crash नहीं होता।
Syntax
cpp
try {
  // risky code
} catch (exception_type variable_name) {
  // handle this type
} catch (...) {
  // handle any other exception
}

try-catch क्या है?

invalid user input जैसी unexpected problems अन्यथा एक program को outright crash कर सकती हैं; C++ के try-catch blocks आपको उन situations को gracefully handle करने देते हैं।

आप risky code को try block के अंदर रखते हैं और matching catch block के अंदर recovery logic लिखते हैं।

उदाहरण: What is try-catch?

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 1;
	// Handle the exception if one was thrown
	} catch (int e) {
		// Print to the console with cout
		std::cout << "Handled gracefully" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Integers Throw और Catch करना

C++ actually आपको लगभग कोई भी value throw करने देता है, और एक plain integer throw करना एक error code signal करने के सबसे simple तरीकों में से एक है। इसे receive करने वाला catch block एक function parameter जैसा behave करता है, जो भी value thrown हुई उसे capture करते हुए।

उदाहरण: Throwing and Catching Integers

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 404;
	// Handle the exception if one was thrown
	} catch (int errorCode) {
		// Print to the console with cout
		std::cout << "Error code: " << errorCode << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Exceptions Catch करना

Standard header <stdexcept> ready-to-use exception classes से भरा है जो सब std::exception से inherit करती हैं और एक handy what() method expose करती हैं जो गलत हुई चीज़ का एक human-readable description return करती है।

उदाहरण: Catching 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("Failure");
	// 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 Blocks

एक single try block के बाद कई catch blocks हो सकते हैं, और C++ order में thrown exception को हर एक के declared type से match करता है, सिर्फ पहला match करने वाला execute करते हुए और बाकी को पूरी तरह skip करते हुए।

उदाहरण: Multiple Catch Blocks

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" << std::endl;
	// Handle the exception if one was thrown
	} catch (double e) {
		// Print to the console with cout
		std::cout << "double: " << e << std::endl;
	}
	return 0;
}

Catch-All Block

तीन dots (...) से एक specific type की बजाय लिखा गया एक catch-all block किसी भी exception type को catch करता है जो पहले के एक ज़्यादा specific catch block से पहले से handled नहीं था, एक last line of defense की तरह act करते हुए ताकि एक unexpected error type program crash न करे।

उदाहरण: The 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 by fallback" << 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. const& की बजाय एक exception को value से catch करना, जो इसे copy करता है और derived exceptions slice कर सकता है।
  2. ज़्यादा specific वालों से पहले एक general catch (exception&) रखना ताकि वे कभी न चलें।
  3. एक try या catch body के आस-पास braces भूल जाना।
🔒

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.