Exception Handling in C++

Estudies4you

 EXCEPTION HANDLING 

Q1. Define exception handling.  
Answer: 
Exception handling in C++ handles only synchronous exceptions. C++ exception handling mechanism uses three keywords: try, catch and throw. The block of statements that may throw exceptions are put inside the try block. When an exception is detected, it is thrown using a throw statement in the try block. The exceptions generated in try block and thrown using throw statement are caught and handled in the catch block. 
When the try block throws an exception then the control leaves the try block and goes to the catch block. Exceptions are nothing but the objects that are used to transmit information about a problem. If exception thrown matches the argument type in the catch block then the catch block is executed which reports the error. If exception thrown does not match the argument type in the catch block then the program is aborted by default using abort() function. If no exception is thrown in the try block then the control goes to next statement immediately following the catch block by skipping the catch block. 

Q2. Define exception and its types. 
Answer: 
Exceptions 
Exceptions are the run-time errors that occurs during the program execution. 
Reasons for Exception Occurrence 
Exception occurs due to,
(i) Division-by-zero condition 
(ii) Exceeding the bounds of an array 
(iii) Running out of memory 
(iv) Falling short of memory 
(v) Object initialization to an impossible value.   
These exceptions can be handled in a systematic way by using the three keywords try, throw and catch. 
Types of Exceptions 
Various types of exceptions are illustrated as follows, 
Exception 
Synchronous exception
Asynchronous exception
Errors like out of range array index and overflow are the types of synchronous exception. The errors encountered due to the occurrence of the events that are not under the control of the program are called asynchronous exceptions 

Q3. Write about throwing an exception. 
Answer: 
An exception detected in try block is thrown using "throw" keyword. An exception can he thrown using throw statement in following number of ways, 
throw(exception); 
throw exception; 
throw;
Where, 'exception' is an object of any type including a constant. The third form of throw statement is used in rethrowing of an exception. The objects that are intended for error handling can also be thrown. The point at which an exception is thrown is called a throw-point. When exception is thrown the control leaves the try block and it reaches to the catch block associated with the try block where the exception is handled. 
The throw point can be in a nested function call or in a nested scope within a try block. In any one of these cases the control is transferred to the catch statement. 

Q4. Write short notes on try block. 
Answer: 
Try is a keyword that is used to detect the exceptions i.e., run time errors. The statements that generates exceptions are • kept inside the try block. The runtime errors can be handled and prevented using try block. The general syntax of try block is as follows, 
try 
{
//code 
throw exception 
}
A try block can throw more than one exception. There should be a catch block to handle the exceptions thrown by try block. 

Q5. Discuss about catch block. 
Answer : 
The catch block handles an exception thrown by the try block. It defines actions to be taken when a run time error occurs. The general syntax of catch block is shown below, 
catch(type argument) 
{
//code 
}
A catch block takes an argument as an exception. These arguments specify the type of an exception that can be handled by the catch block. When an exception is thrown the control goes to catch block. If the type of exception thrown matches the argument then the catch block is executed, otherwise the program terminates abnormally. If no exception is thrown from the try block then the catch block is skipped and control goes immediately to the next statement following the catch block. 
To Top