functional::bad_function_call in C++ with Examples

Standard C++ contains several built-in exception classes, functional::bad_function_call is one of them. This is an exception thrown on bad call. Below is the syntax for the same:

Header File:

include<functional>

Syntax:

class bad_function_call;

Note: To make use of functional::bad_function_call, one should set up the appropriate try and catch blocks.

Below are the programs to understand the implementation of functional::bad_function_call in a better way:



Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for functional::bad_function_call
#include <bits/stdc++.h>
  
using namespace std;
  
// main method
int main()
{
    function<int()> gfg = nullptr;
  
    // try block
    try {
        gfg();
    }
  
    // catch block to handle the errors
    catch (const bad_function_call& geeksforgeeks) {
        cout << geeksforgeeks.what() << endl;
    }
    return 0;
}

chevron_right


Output:

bad_function_call

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for functional::bad_function_call
#include <bits/stdc++.h>
  
using namespace std;
  
// main method
int main()
{
    function<int()> geeksforgeeks = nullptr;
  
    // try block
    try {
        geeksforgeeks();
    }
  
    // catch block to handle the errors
    catch (const bad_function_call& gfg) {
        cout << gfg.what() << endl;
    }
    return 0;
}

chevron_right


Output:

bad_function_call

Reference: http://www.cplusplus.com/reference/functional/bad_function_call/

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.