2013-12-12 151 views
2

对于我在Java中的最终测试,我们在测试中使用try,catch和finally调用了一个“exceptions”部分。当我尝试将示例代码放入Eclipse中时,我会在catch中发现错误并抛出新的区域。所有错误都会显示“无法解析输入”。IOException无法解析为类型错误

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

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

+3

'import java.io.IOException' –

回答

12

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

+0

这真的值得更多upvotes。 – Panzercrisis

2

哦,我想我能在这里回答我的问题。 不知道我不得不从java.io导入IOException!

+1

正确。除了'java.lang'包下的类以外,您需要显式导入或输入完全限定的类。 –

0

简单,只需使用

import java.io.* 

的进口

相关问题