C++ में Exception Hierarchy
In this page:
#include <stdexcept>
try {
throw std::runtime_error("message");
} catch (const std::exception &e) {
e.what();
}
Standard Exceptions का Overview
C++ के standard exceptions std::exception पर rooted एक hierarchy बनाते हैं, जो दो broad categories में branch होता है: unpredictable external factors से cause हुई problems के लिए std::runtime_error, और एक careful programmer जिन्हें पहले से रोक सकता था उन bugs के लिए std::logic_error।
उदाहरण: Overview of 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("Runtime issue");
// 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
Runtime Errors (runtime_error)
एक runtime_error एक ऐसी failure represent करता है जिसे अकेले logic checks reliably पहले से predict नहीं कर सकते — जैसे एक network timeout, एक hardware fault, या एक full disk, जो program के अपने control से बाहर की conditions पर depend करते हैं।
उदाहरण: Runtime Errors (runtime_error)
// 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("Network timeout");
// Handle the exception if one was thrown
} catch (std::runtime_error &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
Logic Errors (logic_error)
इसके contrast में एक logic_error, एक ऐसी mistake represent करता है जो correct code से theoretically avoidable थी — एक invalid argument pass करना, या एक container element access करने की कोशिश करना जो range से बाहर है, दोनों preventable logic errors के classic examples हैं।
उदाहरण: Logic Errors (logic_error)
// 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::invalid_argument("Invalid argument passed");
// Handle the exception if one was thrown
} catch (std::logic_error &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
Base Reference से Catch करना
आपको हमेशा exceptions को value की बजाय const reference से catch करना चाहिए, क्योंकि reference से catch करना object का real polymorphic type preserve करता है और object slicing avoid करता है, जो अन्यथा एक derived exception को सिर्फ इसके base class हिस्से तक काट देगा।
उदाहरण: Catching by Base Reference
// 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::out_of_range("Index out of range");
// Handle the exception if one was thrown
} catch (const 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
Bad Allocations (bad_alloc)
std::bad_alloc सीधे standard library के memory allocator से throw होता है — सबसे common new operator से triggered — जब भी system genuinely free heap memory खत्म कर देता है और एक allocation request बस satisfy नहीं हो सकती।
उदाहरण: Bad Allocations (bad_alloc)
// Include std::cout and std::cin
#include <iostream>
#include <new>
// Program execution starts in main()
int main() {
// Try code that might throw an exception
try {
// Throw an exception to signal an error
throw std::bad_alloc();
// Handle the exception if one was thrown
} catch (std::bad_alloc &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
- एक derived से पहले एक base exception catch करना, ताकि derived handler कभी न चले।
- reference की बजाय value से catch करना, जो exception slice करता है।
runtime_errorयाlogic_errorइस्तेमाल करने से पहले#include <stdexcept>भूल जाना।
- Exception handling program crash किए बिना runtime errors से deal करता है।
try,catch, औरthrowexceptions signal और handle करते हैं।- Custom exceptions और एक exception hierarchy आपको meaningful error types बनाने देते हैं।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: