C++ Exception Handling का परिचय
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Exceptions Throw करना
throw keyword यह signal करने का तरीका है कि एक runtime error हुई है; इसके execute होते ही, normal program flow तुरंत रुक जाता है और control सीधे उस nearest catch block पर jump करता है जिसका type थ्रोन चीज़ से match करता है।
उदाहरण: Throwing Exceptions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 (...)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक exception को value से catch करना, जो slice करता है।
- specific catches से पहले एक general
catch(...)रखना। - कुछ भी catch न करना, ताकि program terminate हो जाए।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: