'Exception cannot be converted to Throwable

I'm working on macOS with JDK8.

In catch, I have to give the entire name of exception like in this case (ArithmeticException e) instead of (Exception e) to run the code.

If I use (Exception e) it gives an error that I'm not getting on windows os.

why is that?? and how should I solve this??

The code works perfectly on windows OS with JDK8. On macOS work perfectly if the proper name of the exception (ArithmeticException e) is given.

import java.util.*;
public class ExceptionDemo
{
public static void main(String args[])
{
   int a,b,c;
   Scanner sc=new Scanner(System.in);
   System.out.println("enter first number:");
   a=sc.nextInt();
   System.out.println("enter second number:");
   b=sc.nextInt();
   try
   {
       c=a/b;
       System.out.println("Result is:"+c);
   }
   catch(Exception e)
   {
       System.out.println("second number cannot be zero/0 "+e);
   }

   System.out.println("still running");
   }
   }

This is the error I'm getting as below:

incompatible types: Exception cannot be converted to Throwable catch(Exception e)



Solution 1:[1]

catch(java.lang.Exception e) {
    // handle e
}

Use fully-qualified names if you aren't sure what is imported, and what class will be used under the name Exception.

The name Exception looks too broad for your application. [YourApplicationName]Exception would be a cleaner and conflictless root of your exception hierarchy (if you want to have one).

Solution 2:[2]

I was having the same problem, but later I noticed that my class name was Exception because of my class name which is(Exception ) I was getting that error .when I changed my class name (filename ) then it work.

Solution 3:[3]

If there is any file named "Exception.java" then rename that file and delete that class file then it will work fine.

Solution 4:[4]

The problem is your file name is Exception.java and the class you are trying to implement is the same, but from a different package (java.lang.Exception), so either you change your file name, or you can import that package, or you can write the full package at the catch param block.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Tobilko
Solution 2 Anish Kumar
Solution 3 Sanjay Sanjay
Solution 4 Konrad Höffner