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

C++ का throw Statement

throw है कि आप कुछ गलत होने पर खुद एक alarm कैसे raise करते हैं, help के लिए चिल्लाने जैसा। कहीं और का कोई code उस alarm को catch करके इससे deal कर सकता है।
Syntax
cpp
throw value;

try {
  throw value;
} catch (value_type variable_name) {
  // handle it
}

throw Keyword

throw keyword explicitly एक exception raise करता है: एक throw statement execute होते ही, C++ execution का normal flow छोड़ देता है और call stack में ऊपर उस nearest catch block को ढूंढना शुरू करता है जिसका type thrown चीज़ से match करे।

उदाहरण: The throw Keyword

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 5;
	// 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;
}

अलग Data Types Throw करना

आप primitive values, pointers, strings, या पूरी तरह custom types throw करने के लिए free हैं, लेकिन एक bare primitive की बजाय एक proper exception object throw करना ज़्यादातर real C++ code का follow किया practice है, क्योंकि यह failure के बारे में कहीं ज़्यादा descriptive information carry कर सकता है।

उदाहरण: Throwing Different Data Types

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

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw std::string("Something went wrong");
	// 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;
}

Objects Throw करना

Objects throw करना खासकर useful है क्योंकि object खुद गलत हुई चीज़ के बारे में structured details carry कर सकता है — एक error code, एक message, related state — आखिरकार इसे catch करने वाले code को एक bare error number से कहीं ज़्यादा काम करने के लिए देते हुए।

उदाहरण: Throwing Objects

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

// Define the type MyError
class MyError {
// Members below can be accessed from outside the class
public:
	std::string message;
	MyError(std::string msg) : message(msg) {}
};

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		// Throw an exception to signal an error
		throw MyError("Custom error object");
	// Handle the exception if one was thrown
	} catch (MyError &e) {
		std::cout << e.message << std::endl;
	}
	return 0;
}

एक Exception Rethrow करना

कभी-कभी एक catch block सिर्फ partially एक error handle कर सकता है और एक higher-level handler को बाकी से deal करने देना ज़रूरी है; आप बस बिना किसी operand के throw लिखकर currently handled exception rethrow कर सकते हैं, जो original exception object preserve करता है।

उदाहरण: Rethrowing an Exception

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

void inner() {
	// 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 << "Inner partially handles it" << std::endl;
		// Throw an exception to signal an error
		throw;
	}
}

// Program execution starts in main()
int main() {
	// Try code that might throw an exception
	try {
		inner();
	} catch (int e) {
		std::cout << "Outer fully handles: " << e << std::endl;
	}
	return 0;
}

noexcept Keyword

noexcept specifier के साथ एक function declare करना callers से promise करता है कि यह कभी एक exception throw नहीं करेगा।

अगर एक noexcept function वह promise तोड़े और फिर भी throw करे, runtime तुरंत std::terminate call करता है, जो stack unwind करने की कोशिश करने की बजाय program खत्म कर देता है।

उदाहरण: noexcept Keyword

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

void safeFunction() noexcept {
	// Print to the console with cout
	std::cout << "Promises never to throw" << std::endl;
}

// Program execution starts in main()
int main() {
	safeFunction();
	// 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. throw new Error() जैसे एक pointer throw करना, जिसे फिर एक pointer की तरह catch करना और delete करना ज़रूरी है।
  2. कोई matching handler के बिना एक exception throw करना, जो std::terminate से program खत्म कर देता है।
  3. rethrow के लिए throw; की बजाय throw e; इस्तेमाल करना, जो copy करता है और exception slice कर सकता है।
🔒

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.