Standard C++ contains several built-in exception classes, exception::bad_exception is one of them. This is an exception thrown by unexpected handler. Below is the syntax for the same:
Header File:
include<exception>
Syntax:
class bad_exception;
Return: The exception::bad_exception returns a null terminated character that is used to identify the exception.
Note: To make use of exception::bad_exception, one should set up the appropriate try and catch blocks.
Below are the examples to understand the implementation of exception::bad_exception in a better way:
Program 1 :
// C++ code for std::bad_exception #include <bits/stdc++.h> using namespace std; void func() { throw; } void geeksforgeeks() throw(bad_exception) { throw runtime_error("test"); } // main method int main() { set_unexpected(func); // try block try { geeksforgeeks(); } // catch block to handle the errors catch (const bad_exception& gfg) { cout << "Caught exception " << gfg.what() << endl; } return 0; } |
Caught exception std::bad_exception
Program 2 :
// C++ code for std::bad_exception #include <bits/stdc++.h> using namespace std; void gfg() { throw; } void A_Computer_Science_Portal_For_Geeks() throw(bad_exception) { throw runtime_error("test"); } // main method int main() { set_unexpected(gfg); // try block try { A_Computer_Science_Portal_For_Geeks(); } // catch block to handle the errors catch (const bad_exception& a) { cout << "Caught exception " << a.what() << endl; } return 0; } |
Caught exception std::bad_exception
Reference: http://www.cplusplus.com/reference/exception/bad_exception/
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.
Recommended Posts:
- std::less in C++ with Examples
- cin get() in C++ with Examples
- Pi(π) in C++ with Examples
- std::equal_to in C++ with Examples
- std::is_trivially_move_constructible in C++ with Examples
- std::is_nothrow_assignable in C++ with Examples
- std::is_default_constructible in C++ with Examples
- std::add_const in C++ with Examples
- std::is_literal_type in C++ with Examples
- std::is_trivially_constructible in C++ with Examples
- std::add_volatile in C++ with Examples
- std::is_copy_assignable in C++ with Examples
- std::is_move_assignable C++ with Examples
- std::bit_or in C++ with Examples
- std::unary_negate() in C++ with Examples
- exception::what() in C++ with Examples
- std::greater in C++ with Examples
- std::is_trivially_destructible in C++ with Examples
- std::is_nothrow_default_constructible in C++ with Examples
- std::is_member_pointer in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

