NPTEL Programming in Java Week 5 Programming Assignment 5 Solution

 In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.

For example, if user’s input is 1, then it will throw and catch java.lang.NullPointerException“.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
public class Question5_5{
3
    public static void main (String   args[ ] ) {
4
           Scanner scan = new Scanner(System.in);
5
            int i = scan.nextInt();
6
        int j;
7
// Put the following code under try-catch block to handle exceptions
8
     try{
9
        switch (i) {
10
        case 0 : 
11
        int zero = 0; 
12
        j = 92/ zero;       
13
        break;
14
            case 1 : 
15
        int b[ ] = null; 
16
        j = b[0] ;  
17
        break;
18
            default:
19
           System.out.println("No exception");
20
        }
21
      } 
22
     catch(Exception e)
23
        {
24
            System.out.print(e);
25
        }
0
}
1
}
Post a Comment (0)
Previous Question Next Question

You might like