C++ में try-catch
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई Catch Blocks
एक single try block के बाद कई catch blocks हो सकते हैं, और C++ order में thrown exception को हर एक के declared type से match करता है, सिर्फ पहला match करने वाला execute करते हुए और बाकी को पूरी तरह skip करते हुए।
उदाहरण: Multiple Catch Blocks
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
const&की बजाय एक exception को value से catch करना, जो इसे copy करता है और derived exceptions slice कर सकता है।- ज़्यादा specific वालों से पहले एक general
catch (exception&)रखना ताकि वे कभी न चलें। - एक try या catch body के आस-पास braces भूल जाना।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: