C++ का throw Statement
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
अलग 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Objects Throw करना
Objects throw करना खासकर useful है क्योंकि object खुद गलत हुई चीज़ के बारे में structured details carry कर सकता है — एक error code, एक message, related state — आखिरकार इसे catch करने वाले code को एक bare error number से कहीं ज़्यादा काम करने के लिए देते हुए।
उदाहरण: Throwing Objects
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Exception Rethrow करना
कभी-कभी एक catch block सिर्फ partially एक error handle कर सकता है और एक higher-level handler को बाकी से deal करने देना ज़रूरी है; आप बस बिना किसी operand के throw लिखकर currently handled exception rethrow कर सकते हैं, जो original exception object preserve करता है।
उदाहरण: Rethrowing an Exception
// 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;
}
Login to try C/C++/Java/PHP code in the editor
noexcept Keyword
noexcept specifier के साथ एक function declare करना callers से promise करता है कि यह कभी एक exception throw नहीं करेगा।
अगर एक noexcept function वह promise तोड़े और फिर भी throw करे, runtime तुरंत std::terminate call करता है, जो stack unwind करने की कोशिश करने की बजाय program खत्म कर देता है।
उदाहरण: noexcept Keyword
// 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;
}
Login to try C/C++/Java/PHP code in the editor
throw new Error()जैसे एक pointer throw करना, जिसे फिर एक pointer की तरह catch करना और delete करना ज़रूरी है।- कोई matching handler के बिना एक exception throw करना, जो
std::terminateसे program खत्म कर देता है। - rethrow के लिए
throw;की बजायthrow e;इस्तेमाल करना, जो copy करता है और exception slice कर सकता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: