NPTEL Programming in Java Week 5 Programming Assignment 3 Solution

 Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
  
3
  public class Question5_3 {
4
  public static void main(String[] args) { 
5
      int a, b;
6
      Scanner input = new Scanner(System.in);
7
//Read any two values for a and b.
8
a = input.nextInt();
9
    b = input.nextInt();
10
11
//Get the result of a/b;
12
try{
13
        System.out.print(a/b);
14
       }catch(ArithmeticException e){
15
          System.out.print("Exception caught: Division by zero.");
16
        }
17
18
 
0
}
1
}
Post a Comment (0)
Previous Question Next Question

You might like