Complete the code to declare a method that throws a checked exception.
public void readFile() throws [1] {
// method body
}The IOException is a checked exception, so it must be declared with throws.
Complete the code to catch an unchecked exception.
try { int result = 10 / 0; } catch ([1] e) { System.out.println("Cannot divide by zero"); }
ArithmeticException is an unchecked exception thrown when dividing by zero.
Fix the error in the code by choosing the correct exception type to declare.
public void connect() throws [1] {
// code that may throw exception
}SQLException is a checked exception and must be declared in the method signature.
Fill both blanks to complete the code that handles a checked exception properly.
try { FileReader file = new FileReader("file.txt"); } catch ([1] e) { System.out.println("File not found: " + e.getMessage()); } finally { [2] }
The FileNotFoundException is a checked exception that must be caught. The file.close() in finally ensures the file resource is closed.
Fill all three blanks to create a method that throws a checked exception and handles an unchecked exception.
public void process() throws [1] { try { int[] arr = new int[5]; int x = arr[10]; } catch ([2] e) { System.out.println("Index error: " + e.getMessage()); } throw new [3]("File error"); }
The method declares it throws FileNotFoundException (a checked exception). It catches ArrayIndexOutOfBoundsException (an unchecked exception) inside the try-catch block. Then it throws a new FileNotFoundException.