13.1

Write a program containing a possible exception. Use a try block to throw it and a catch block to handle it promptly.

Answer

OUTPUT

Enter power factor 2

Caught an exception
13.2

Write a program that illustrates the application of multiple catch statements.

OUTPUT

How many elements do you want to enter ? 20

array size must be less than or equal 5
13.3

Write a program which uses catch(…) handler.

Answer

OUTPUT

How many elements do you want to enter ?  10

an exception is detected
13.4

Write a program that demonstrates how certain exception types are not allowed to be thrown.

Answer

OUTPUT

In empty()

Caught an int  = 1
14.5

Write a program to demonstrate the concept of re-throwing an exception.

Answer

OUTPUT

Enter the value of a & b :  200   0

Caught an exception as first throwing
Caught an exception as rethrowing
13.6 Write a program with the following:
  • (a) a function to read two double type numbers from keyboard.
  • (b) a function to calculate the division of these two numbers.
  • (c) a try block to throw an exception when a wrong type of data is keyed in.
  • (d) A try block to detect and throw an exception if the condition “divide by zero” occurs.
  • (e) Appropriate catch block to handle the exception thrown.
Answer

OUTPUT

Enter two number :   150   0

Caught an exception
13.7

Write a main program that calls a deeply nested function containing an exception. Incorporate necessary exception handling mechanism.

Answer

OUTPUT

Enter a positive number -23

Caught an exception

Next Previous