Exception Handling in C++ Short Questions with Solutions

Estudies4you
Exception Handling in c++ short questions with answers
 Q6. Define exception objects. 
Answer : 
The exception object holds the error information about the exception that had occurred. The information includes the type of error i.e.. logic error or run time error and state of the program when the error occurred. An exception object is created as soon as an exception occurs and it is passed to the corresponding catch block as a parameter. This catch block contains the code to catch the occurred exception. It uses the methods of exception object for handling the exceptions. A base class called std ∷ exception is designed to declare objects to he thrown as exceptions. 

Q7. What are exception specifications? 
Answer : 
Exception Specification 
Exception specification is a feature which specifies the compiler about possible exception 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, it the function does not have any exception specification then it can throw any exception. 

Q8. Define stack unwinding. 
Answer : 
Whenever an exception is thrown, usually the control of program shifts front the try Hoek and looks lot a corresponding handler. During this session the C++ run time calls destructor relative to all automatic objects created while the initiation of try block. This consequence is usually referred as stack unwinding. As a result of it, all the automatic objects which were created. get destroyed in the reverse order of creation. 
Assume that the program control is busy in creating an object and this object in turn processes sub-objects or array elements Now consider that an exception is raised while this process is in progress. To manage such consequences, destructors are called for those sub-objects/array elements which were executed successfully prior to the raising of any exception. 
The terminate function is called on the account that the destructor throws an exception and there is none to handle this exception. while the stack unwinding process is in progress. 

Q9. Define rethrowing an exception. 
Answer : 
In the program execution, when an exception received by catch block is passed to another exception handler then such situation is referred to as rethrowing of exception. 
This is done with the help of following statement, 
throw; 
The above statement does not contain any arguments. This statement throws the exception to next try catch block. 

Q10. Define catching all exceptions. 
Answer : 
Catching Multiple Exceptions 
In C++, a user can catch all exceptions simultaneously. Inorder to do this, a single catch block is defined for catching all the exceptions thrown by using different throw statements. This catch block is of generic type. 
Syntax 
catch 
{
//statements for handling various exceptions
}

To Top