简体   繁体   English

异常无法转换为 Throwable

[英]Exception cannot be converted to Throwable

I'm working on macOS with JDK8.我正在使用 JDK8 在 macOS 上工作。

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.在 catch 中,我必须像在这种情况下一样给出异常的完整名称 (ArithmeticException e) 而不是 (Exception e) 来运行代码。

If I use (Exception e) it gives an error that I'm not getting on windows os.如果我使用 (Exception e),它会给出一个错误,表明我没有在 Windows 操作系统上运行。

why is that??这是为什么?? and how should I solve this??我应该如何解决这个问题?

The code works perfectly on windows OS with JDK8.该代码在带有 JDK8 的 Windows 操作系统上完美运行。 On macOS work perfectly if the proper name of the exception (ArithmeticException e) is given.如果给出异常的正确名称 (ArithmeticException e),则在 macOS 上可以完美运行。

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)不兼容的类型:异常无法转换为 Throwable catch(Exception e)

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 .如果您不确定导入的内容以及将在名称Exception下使用的类,请使用完全限定的名称。

The name Exception looks too broad for your application.名称Exception对于您的应用程序来说看起来太宽泛了。 [YourApplicationName]Exception would be a cleaner and conflictless root of your exception hierarchy (if you want to have one). [YourApplicationName]Exception将是您的异常层次结构的更清晰且无冲突的根(如果您想要一个)。

我遇到了同样的问题,但后来我注意到我的类名是 Exception 因为我的类名是(异常)我收到了那个错误。当我改变我的类名(文件名)时,它就起作用了。

如果有任何名为“Exception.java”的文件,则重命名该文件并删除该类文件,然后它将正常工作。

你应该先导入

import java.lang.Exception;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM