Question: Complete the program below in order to make run properly by adding try-catch blocks in the main method. public class ExceptionWithThrow { public static Scanner

Complete the program below in order to make run properly by adding try-catch blocks in the main method.

public class ExceptionWithThrow {

public static Scanner openFile(String fileName) throws FileNotFoundException{ FileReader fr = new FileReader (fileName); Scanner sc = new Scanner (fr); return sc;

} public static boolean isValidIdentifier(String name){

// check here if the name is a valid identifier name

}

public static String readValidIdentifierName() throws InputMismatchException{ Scanner input = new Scanner (System.in); String name = input.next(); if ( ! isValidIdentifier() )

throw new InputMismatchException();

return name; }

public static int readInteger() throws InputMismatchException { Scanner input = new Scanner (System.in); int num = input.nextInt(); return num;

}

public static boolean isDivisible(int x, int y) throws ArithmeticException { if (y == 0)

throw new ArithmeticException(); if (x % y == 0)

return true; else

return false;

}

public static void main(String[] args) {

// Add try and catch blocks around appropriate statements // Two exceptions are expected hereSystem.out.println("Please enter the input file name:"); String fName = readValidIdentifierName();

Scanner fsc = openFile(fName);

// One exception is expected here

System.out.println("Please give me an integer number"); int firstNum = readInteger(); System.out.println("Please give me another integer number"); int secondNum = readInteger();

// One exception is expected here

if (isDivisible(firstNum, secondNum)){ System.out.println(firstNum + " is divisible by " + secondNum);

} else{

System.out.println(firstNum + " is NOT divisible by " + secondNum); }

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!