13.1

What is an exception?

Answer

Exceptions are run-time anomalies or unusual conditions that a program may encounter while executing.

13.2

How is an exception handled in C++?

Answer

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:

Exception

13.3

What are the advantages of using exception handling mechanism in a program?

Answer

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.

13.4

When should a program thrown an exception?

Answer

When an exception is detected in try block then a program should throw an exception to catch block.

13.5

When is a catch(…) handler is used?

Answer

To catch all exceptions catch (…..) handler is used.

13.6

What is an exception specification? When is it used?

Answer

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)

{

function body

}

13.7

What should be placed inside a try block?

Answer

A throw statement should be placed inside a try block.

13.8

What should be placed inside a catch block?

Answer

A appropriate massage for which an exception is caught should be placed in catch block.

13.9

When do we used multiple catch handlers?

Answer

When there are more than one exception in a program, then we use multiple catch statements.

13.10

Missing…

Answer
  • (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.
  • (c)

General form :

try

{

//try block

}

catch (type1 arg)

{

//catch block 1

}

catch (type2 arg)

{

// catch block 2

}

 

…………

…………

catch (typeN arg)

{

//catch blockN

}

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.
13.11

Missing…

Answer
  • (a) A handler may decide to rethrow the exception caught without processing it. In such situation we use;

throw;

  • (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.

 


Next Previous