Java Exception throw statement with primitive value

Question

What is the output of the following code?

public class Main {
  public static void main(String args[]) {
    try {/*from   w  w w  .j  a  v a  2 s.  c  o m*/
      int a = 0;
      int b = 42 / a;
    } catch (Exception e) {
      System.out.println("got one");
      throw 0;
    } 
  }
}


Compile time error
No exception of type int can be thrown; 
an exception type must be a subclass of Throwable

Note

We can only throw an object of type Throwable or a subclass of Throwable.

Primitive types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions.




PreviousNext

Related