Exception Specifications in C++
INTRODUCTION
A component known as exception definition informs the interpreter of potential exceptions that an operation could raise.
Syntax:
The syntax for the exception specification is as follows:
return_type FunctionName(arg_list) throw (type_list)
The type for the purpose is represented by "return_type" in the preceding notation. FunctionName denotes the operation's title, arg_list denotes the set of parameters supplied through the operation, throw denotes the term that allows the method to throw an error, and type_list denotes the table of all exception kinds. A null exception declaration might indicate every function that cannot throw any errors. The unforeseen method is invoked when an action throws an error that doesn't exist in the error definition. This process ends the application. Without an error definition within an aspect declaration, the method might throw every possible exception.
Examples of different exceptions:
Void verify (int s)
Verify () is the title for the operation, while s represents the parameter for the instance mentioned below. Void is the result's value. The method verifies () raises an error of kind 'int' after the error requirements, which will be followed by throw (int).
Example program:
#include <iostream>
#include <stdexcept>
void verify(int s) {
if (s < 0) {
throw std::runtime_error("Invalid value for the int s is given");
}}
int main() {
try {
verify(-5);
} catch (const std::exception& e) {
std::cout << "Exception found: " << e.what() << std::endl;
}
return 0;
}
Output:
Void verify (int p) throw ()
Since the operation's problem definition is blank in this case, it can throw any possible exception.
C++ provides a technique to guarantee whether a method can only be configured to throw exceptions within a predefined range. Every function that starts with an exception description assures the method's user that its execution only intends to throw the errors listed in the exception declaration.
Example program:
#include <iostream>
void verify(int p) throw () {
if (p < 0) {
std::cerr << "The Invalid value is given" << std::endl;
}}
int main() {
try {
verify(-55); // Because of the absence of an error definition, this isn't going to generate an exception.
} catch (...) {
std::cerr << "Exception is found:" << std::endl;
}
return 0;
}
Output:
Syntax of working of exception specification.
The picture shows the syntax working of the exception specification.
The collection of kinds in the type_id_list has been delimited by a comma. Any unfinished kind, class abstract kind, C++11 connection kind, or port or connection to an unfinished form apart from those listed below are not specified in the following list.
Whatever exception is permitted by a call to a function that doesn't specify any exemptions. Throw (), a method function that incorporates a void type_id_list in its occurrence standards prevents the throwing of every possible exception. A function's class does not include a special-case definition.
Example for the syntax void (*g) () throw(int);
#include <iostream>
void foo() throw() {
throw 42; // throwing an error instead of a number.
}
int main() {
void (*g)() throw(); // Make an object pointer declaration that includes the requested exception information.
g = foo; // Put "foo" method as an object pointer.
try {
g(); // Utilize the method pointer to invoke its purpose.
} catch (int e) {
std::cout << "Exception caught: " << e << std::endl;
}
return 0;
}
Output:
Example for the syntax void h (void i() throw(int));
#include <iostream>
void foo() throw() {
throw 42; // raising an exception of a numerical value
}
void h(void (*i)() throw()) {
try {
i(); // Use the method pointer supplied as an argument to execute the given method.
} catch (int e) {
std::cout << "Exception caught: " << e << std::endl;
}}
int main() {
h(foo); // Pass the "foo" function to the "h" function
return 0;
}
Output:
Examples that raise specific exceptions:
class ALPHA {
public:
struct ALPHA_ERR {};
virtual void fun(void) throw() {
} // Exception specification
};
class BETA: public ALPHA {
void fun(void) throw() {
} // Exception specification is changed};
ERROR PRODUCED: