Java - What is the output: particular exception

Question

What is the output?

import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    int x = 10, y = 0, z = 0;
    try {
      z = x / y;
    } catch (IOException e) {
      // Handle exception
    }
  }
}


Click to view the answer

} catch (IOException e) { //exception java.io.IOException is never thrown in body of corresponding try statement

Note

If the code in a try block would not throw a checked exception and its associated catch blocks catch that exceptions, the compiler will generate an error.

Here is the fix

Demo

public class Main {
  public static void main(String[] args) {
    int x = 10, y = 0, z = 0;
    try {/*  ww w . j  a v  a  2s . co  m*/
      z = x / y;
    } catch (Exception e) {
      // Handle the exception
    }
  }
}