try { // some code that may throw an exception } catch (std::exception& e) { // handle the exception }
class MyException : public std::exception { public: virtual const char* what() const throw() { return "My custom exception"; } }; void foo() { throw MyException(); }In this example, we define a custom exception class, MyException, which inherits from std::exception. We override the what() method to return a custom message, and then throw an instance of the MyException class in the foo() function. Package library: The C++ std exception library is part of the standard C++ library, which is a core part of the C++ language itself. There is no need to install or import any external libraries to use the std exception library.