问题
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)
回答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).
回答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.
回答3:
you should import first
import java.lang.Exception;
来源:https://stackoverflow.com/questions/57623335/exception-cannot-be-converted-to-throwable