【问题标题】:IOException can not be resolved to a type errorIOException 无法解析为类型错误
【发布时间】:2013-12-12 18:35:04
【问题描述】:

对于我的 Java 期末考试,我们在测试中有一个“异常”部分,包括 try、catch 和 finally 调用。当我尝试将示例代码放入 Eclipse 时,我在 catch 中遇到错误并抛出新的区域。所有错误都显示“无法解析输入”。

我该如何解决这个问题,以便我可以了解/查看代码应该做什么?

第四节课

public static void main(String [] args) 
 { 
 Q4Exception q1 = new Q4Exception(); 

 try{ 
 q1.sampleMethod(); 

 try{ 
 q1.sampleMethod(); 
 }
 //This catch does not throw an error 
 catch(RuntimeException es) 
 { 
 System.out.println("A"); 
 }
 //This catch below throws the error of cannot be resolved to a type 
 catch(IOException es) 
 { 
 System.out.println("B"); 
 }
 //This catch does not throw an error 
 catch(Exception e) 
 { 
 System.out.println("C"); 
 } 
 finally{ 
 System.out.println("D"); 
 } 

 }catch(Exception e) 
 { 
 System.out.println("E"); 
 } 
 finally{ 
 System.out.println("F"); 
 } 
 }

Q4异常类

public void sampleMethod() throws Exception 
 { 
 try{ 
 throw new IOException("H"); 
 } 
 catch(IOException err) 
 { 
 System.out.println("I"); 
 throw new RuntimeException("J"); 
 } 
 catch(Exception e) 
 { 
 System.out.println(e.toString()); 
 System.out.println("K"); 
 throw new Exception(“L"); 
 } 
 catch(Throwable t) 
 { 
 System.out.println("M"); 
 } 
 finally{ 
 System.out.println("N"); 
 } 
 } 

【问题讨论】:

  • 你导入IOException了吗?
  • import java.io.IOException

标签: java exception try-catch


【解决方案1】:

哦,我想我可以在这里回答我自己的问题。 不知道我必须从 java.io 导入 IOException!

【讨论】:

  • 正确。除了java.lang 包下的类,您需要显式导入或键入所有类的完全限定。
【解决方案2】:

我认为值得一提的是,在 Eclipse 中,Ctrl+Shif+O 会为您解决导入问题。

【讨论】:

    【解决方案3】:

    简单易用

    import java.io.*
    

    进口

    【讨论】:

      【解决方案4】:

      我发现我使用的是旧版本的 JWT,使用新版本的 JWT 依赖后问题就消失了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        • 2015-11-13
        • 2014-03-19
        相关资源
        最近更新 更多