Exception Specifications in C++

Estudies4you
Notes on Exception Specifications in C++

EXCEPTION SPECIFICATIONS 

Q17. Describe exception specifications in detail. 
Answer: 
Exception Specification 
Exception specification is a feature which specifies the compiler about possible exception to be thrown by a function. 
Syntax 

return_type FunctionName(arg_list) throw (type_list) 

In the above syntax 'return_type' represents the type of the function. FunctionName represents the name of the function, arg_list indicates list of arguments passed to the function, throw is a keyword used to throw an exception by function and type_list indicates list of exception types. An empty exception specification can indicates that a function cannot throw any exception. if a function throws an exception which is not listed in exception specification then the unexpected function is called. This function terminates the program. In the function declaration, if the function does not have any exception specification then it can throw any exception. 

Example 
1. void verify(int p) throw(int) 
In the above example, void is the return type, and verify() is the name of the function, int p is the argument. It is followed by exception specification i.e., throw(int), the function verify() is throws an exception of type 'int'. 

2. void verify(int p) throw() 
Here, the function can throw any exception because it has empty exception specification. 

Program 

#include<iostream> 

#include<exception> 

using namespace std; 

void verify(int p) throw(int) 

{

if(p==1) 

throw p; 

cout<<"\nEnd of verify().function( )"; 

int main() 

try 

{

cout<<p==1\n";

verify(1);

}

catch(int i)

{

cout<<"Interger exception is caught \n";

}

cout<<"\nEnd of main() function";

//getch();

return 0;

}

Output 
Examples of Exception Specifications in c++,program for Exception Specifications in c++,Exception Specifications in c++ syntax,syntax for Exception Specifications in c++,define Exception Specifications,Exception Specifications definition,use of Exception Specifications in c++,benifits of Exception Specifications in c++,role of Exception Specifications in c++,Exception Specifications uses
To Top