BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_badfunctioncall

Provide an exception class thrown by bsl::function.

Outline

Purpose

Provide an exception class thrown by bsl::function.

Classes

Canonical Header

bsl_functional.h

See also
bslstl_function, bslstl_stdexceptionutil

Description

This component provides a bsl::bad_function_call exception class when exceptions are enabled, and nothing otherwise. This exception is thrown by bsl::function::operator() when the function wrapper object has no target. If compiling as C++11 or later, bsl::bad_function_call is an alias for std::bad_function_call.

Usage

This section illustrates intended use of this component.

Example 1: A Degenerate Function Wrapper

Suppose we want to write a null functor, the functional equivalent of a null pointer, which throws an exception when invoked. We can use the bsl::bad_function_call exception type as our common vocabulary for reporting the invocation of functors when they are not in a callable state.

First, we define the class and give it a call operator that unconditionally throws:

struct EmptyFunction {
void operator()() const { BSLS_THROW(bsl::bad_function_call()); }
};
#define BSLS_THROW(X)
Definition bsls_exceptionutil.h:374

Then, we invoke the call operator inside a try block and confirm that the expected exception type is caught:

bool caught = false;
try {
EmptyFunction()();
}
catch (const bsl::bad_function_call&) {
caught = true;
}
assert(caught);