Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception cannot be converted to Throwable

Tags:

java

exception

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)

like image 916
Siddhesh Jadhav Avatar asked Aug 13 '26 03:08

Siddhesh Jadhav


1 Answers

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).

like image 128
Andrew Tobilko Avatar answered Aug 15 '26 11:08

Andrew Tobilko