2013-12-12 146 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.* 

的進口

相關問題