What is an exception?
Exceptions are run-time anomalies or unusual conditions that a program may encounter while executing.
How is an exception handled in C++?
C++ exception handling mechanism is basically built upon the three keywords, namely, try, tow and catch. The keyword try is used to preface a block of statements which may generate exceptions. This block is known as try block. When an exception is detected, it is thrown using a throw statement in the try block. A catch block defined by the keyword ‘catch’ catches the exception thrown by the throw statement in the try block and handles it appropriately. This relationship is shown below:
What are the advantages of using exception handling mechanism in a program?
We often come across some peculiar problems other than logic or syntax errors. These error occurs at run time and the whole program may be crashed. To void these types of error we use exception handling mechanism in a program.
When should a program thrown an exception?
When an exception is detected in try block then a program should throw an exception to catch block.
When is a catch(…) handler is used?
To catch all exceptions catch (…..) handler is used.
What is an exception specification? When is it used?
exception specification is a way to restrict a function to throw only certain specified exceptions. The general form of using an exception specification is :
type function (argument list) throw (type-list)
What should be placed inside a try block?
A throw statement should be placed inside a try block.
What should be placed inside a catch block?
A appropriate massage for which an exception is caught should be placed in catch block.
When do we used multiple catch handlers?
When there are more than one exception in a program, then we use multiple catch statements.
- (a) Our desired exception can be restricted from throwing. General form :
type function (arg.list) throw (type-list)
- (b) When no exception is detected and thrown, the control goes to the statement immediately after the catch block.
General form :
catch (type1 arg)
//catch block 1
catch (type2 arg)
// catch block 2
catch (typeN arg)
When several catch statements match the type of an exception, the first handler that matches the exception type is executed.
- (d) In this case the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.
- (e) This function is prevented from throwing any exception.
- (f) catch (…) will catch all exceptions.
- (g) function is restricted from throwing any exception.
- (h) In this case the current exception is thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block.
- (a) A handler may decide to rethrow the exception caught without processing it. In such situation we use;
- (b) void fun 1 (float x) is restricted from throwing any exception because type list of throw is empty.
- (c) When all exceptions should be caught, then catch (…) is used.