Skip to content
Related Articles

Related Articles

functional::bad_function_call in C++ with Examples
  • Last Updated : 28 May, 2020

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 :




// 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;
}
Output:
bad_function_call

Example 2 :




// 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;
}
Output:
bad_function_call

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :