Standard C++ contains several built-in exception classes, std::bad_weak_ptr is one of them. std::bad_weak_ptr is the type of the object thrown as exceptions by the constructors of shared_ptr that take weak_ptr as the argument, when the weak_ptr refers to an already deleted object. Below is the syntax for the same:
class bad_weak_ptr: public exception;
Note: To make use of std::bad_weak_ptr, one should set up the appropriate try and catch blocks.
Below are the programs to understand the implementation of std::bad_weak_ptr in a better way:
Program 1 :
Program 2 :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- std::bad_array_new_length class in C++ with Examples
- std::is_trivially_copy_assignable class in C++ with Examples
- C++ boost::dynamic_bitset Class with Examples
- std::uniform_real_ distribution class in C++ with Examples
- Difference between Base class and Derived class in C++
- How to convert a class to another class type in C++?
- std::any Class in C++
- std:: valarray class in C++
- std::hash class in C++ STL
- Array class in C++
- Structure vs class in C++
- std::string class in C++
- Virtual base class in C++
- How to implement our own Vector Class in C++?
- Friend class and function in C++
- What all is inherited from parent class in C++?
- C++ String Class and its Applications | Set 2
- C++ string class and its applications
- Simulating final class in C++
- Why is the size of an empty class not zero in C++?