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 :