异常无法转换为Throwable [英] Exception cannot be converted to Throwable

查看:145
本文介绍了异常无法转换为Throwable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JDK8在macOS上工作.

I'm working on macOS with JDK8.

在捕获中,我必须给出异常的全名,例如在这种情况下(ArithmeticException e)代替(Exception e)运行代码.

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.

如果我使用(异常e),则会出现错误,提示我没有进入Windows操作系统.

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??

该代码在带有JDK8的Windows操作系统上可以完美地工作.如果给出了异常的正确名称(ArithmeticException e),则在macOS上可以正常工作.

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:

不兼容的类型:异常不能转换为Throwablecatch(异常e)

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

推荐答案

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

如果不确定要导入的内容以及将在 Exception 名称下使用的类,请使用完全限定的名称.

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

对于您的应用程序来说,名称 Exception 看起来太宽泛了. [YourApplicationName] 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).

这篇关于异常无法转换为Throwable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆