Standard Exception in c++ , std::exception , std::bad_alloc , Member functions , Example

std::exception , std::bad_alloc , Member functions , Example , Standard Exception in c++ :-
इससे पहले के article मे , c++ language मे munual Exceptions handeling को discuss किया है अब इस article मे , c++ language का stanadard Exceptions को discuss करेगे | c++ language मे , कुछ Exceptions को पहले से define किया गया है जिसे किसी प्रोग्राम मे डायरेक्ट ही use किया जाता है | इन Exceptions को standard Exceptions कहते है | इसके लिए Exceptions file को किसी प्रोग्राम  के header portion मे add किया जाता है | इसके बाद ही Exceptions को use किया जा सकता है | नीचे सभी Exception को discuss किया गया है :-Sr.No Exception & Description
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 करेगे |