हिंदी माध्यम नोट्स
Standard Exception in c++ , std::exception , std::bad_alloc , Member functions , Example
1. std::exception
ये Exceptionsclass है जिसमे सभी standard C++ exceptions को define किया गया होता है |
2. std::bad_alloc
std::bad_alloc एक object है जिसे exceptions की तरह से किसी बलको मे फेके जाता है | ये exceptions तब execute होता है तब किसी variable के लिए space को allocate करने में failure आता है |
Member functions
इसमें निन्म member function होता है :-
constructor: इस use bad_alloc object को construct करने के लिए किया जाता है | ये constructor है |
operator : इसका use , किसी bad_alloc object को replace करने के लिए किया जाता है | ये public member function है |
what : इसमें explanatory string को return किया जाता है | ये public member function है |
std::bad_alloc::bad_alloc मे
bad_alloc();
इसका use किसी new bad_alloc object को construct करने के लिए किया जाता है जिसमे implementation-defined null-terminated byte string को what() function से throw किया जाता है |
std::bad_alloc::operator= मे
bad_alloc& operator=( const bad_alloc& other );
Parameters
other – इसमें किसी new object को assign किया जाता है |
Return value
*this
std::bad_alloc::what मे ,
Parameters
इसमें कोई nभी parameter नहीं होता है |
Return value
इसमें pointer return होता है जो की किसी null-terminated string को point करता है इसमें किसी Exceptions की information होती है |
Example
Run this code
#include <iostream>
#include <new>
int main()
{
try {
while (true) {
new int[1000bl];
}
} catch (const std::bad_alloc& e) {
std::cout << “Memory Allocation failed: ” << e.what() << ‘\n’;
}
}
इसमें किसी integer मे alphanumerical value को assign किया जाता है |
3. std::bad_cast
इस type मे , किसी dynamic_cast को throw किया जाता है क्योकि dynamic_cast से किसी reference type के run time मे error occur हो जाती है | आगे के article मे dynamic_cast को भी discuss करेगे |
Member functions
इसमें निन्म member function होता है :-
constructor: इस use bad_cast object को construct करने के लिए किया जाता है | ये constructor है |
what : इसमें explanatory string को return किया जाता है | ये public member function है |
destructor : इसका use किसी constructor को destroy करने के लिए किया जाता है |
std::bad_cast::bad_cast मे
bad_cast();
इसका use किसी new bad_cast object को construct करने के लिए किया जाता है जिसमे implementation-defined null-terminated byte string को what() function से throw किया जाता है |
Example:
#include <iostream>
#include <typeinfo>
struct F { virtual ~Food() {} };
struct B{ virtual ~Bar() {} };
int main()
{
Bar b;
try {
Foo& f1 = dynamic_cast<Food&>(b);
}
catch(const std::bad_cast& exe)
{
std::cout << exe.what() << ‘\n’;
}
}
इसमें दो type को define किया है जिसमे एक food और दूसरा bar को define करता है | जब किसी एक type को दुसरे type मे assign किया जाता है और उस समय अगर exceptions आ जाता है तब इस प्रकार के type को call किया जाता है |
4 .std::bad_exception
इस प्रकार के stanadard exceptions type को किसी c++ प्रोग्राम मे unexpected exceptions को handel करने के लिए किया जाता है |
std::bad_exception को c++ प्रोग्राम मे निन्म condition मे use किया जाता है :-
1)अगर std::exception_ptr मे किसी exception के copy को catch किया है और अगर exception objectका copy constructor को std::current_exception से catch किया गया है तब और किसी एक और exception को throw किया गया है तब इस exception को catch करने के लिए इस type को use किया जाता है |
2) जब किसी dynamic exception के specification को define नहीं किया जाट अहै तब std::unexpected से इस exception को बार बार फेके जाता है और अगर इसके specification को throw किया जाता है तब std::bad_exception, std::bad_exception को use किया जाता है |
Member functions
इसमें निन्म member function होता है :-
constructor : इस use bad_exception object को construct करने के लिए किया जाता है | ये constructor है |
operator : इसका use , किसी bad_exception object को replace करने के लिए किया जाता है | ये public member function है |
what : इसमें explanatory string को return किया जाता है | ये public member function है |
std::bad_exception::bad_exception मे
bad_alloc();
इसका use किसी new bad_exception object को construct करने के लिए किया जाता है जिसमे implementation-defined null-terminated byte string को what() function से throw किया जाता है |
Example
#include <iostream>
#include <exception>
#include <stdexcept>
void unwantedexp() { throw; }
void first() throw(std::bad_exception)
{
throw std::runtime_error(“Frist Test”);
}
void main()
{
std::set_unexpected(unwantedexp);
try {
first();
}
catch(const std::bad_exception& exe)
{
std::cerr << “Catch ” << exe.what() << ‘\n’;
}
}
इस article मे , c++ language के कुछ standard exceptions को discuss किया आब आगे के article मे कुछ और exceptions को discuss करेगे |
Recent Posts
Question Tag Definition in english with examples upsc ssc ias state pcs exames important topic
Question Tag Definition • A question tag is a small question at the end of a…
Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)
Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…
Report Writing examples in english grammer How to Write Reports explain Exercise
Report Writing • How to Write Reports • Just as no definite rules can be laid down…
Letter writing ,types and their examples in english grammer upsc state pcs class 12 10th
Letter writing • Introduction • Letter writing is an intricate task as it demands meticulous attention, still…
विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features
continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…
भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC
भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…