IOException无法解析为一种类型错误

3

在我Java的期末考试中,我们有一个“异常”部分,需要使用try、catch和finally调用。当我尝试将示例代码放入Eclipse时,在catch和throw new区域中出现错误。所有错误都显示为“无法解析为类型”。

我该如何修复这个问题,以便我可以学习/复习代码应该做什么?

Q4类

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"); 
 } 
 }

Q4Exception类

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"); 
 } 
 } 

7
你是否导入了 IOException - Reimeus
4
导入Java的输入输出异常(IOException)。 - Paul Samsotha
4个回答

16

我认为值得一提的是,在Eclipse中,按下Ctrl+Shift+O可以帮你解决导入问题。


3

噢,我猜我可以在这里回答自己的问题了。没想到我要从java.io导入IOException!


1
正确。除了java.lang包下的类,您需要显式导入或完全限定类型所有类。 - Pradeep Simha

0

使用简单

import java.io.*

用于导入


0

我发现我正在使用旧版本的JWT,使用较新版本的JWT依赖项后问题已经解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接