2016-04-05 12 views
0

私は質問がありますが、なぜjavaはその例外をスローし続けますか?ストリームに問題はありますか?私はすべてのIOExceptionをハンドリングしたので!報告されていない例外IOException;キャッチされるか、スローされると宣言される必要があります

[[jio0yh.java:12: error: unreported exception IOException; must be caught or declared to be thrown]]>>

これは例外です。ここ

は私のコードです

import java.io.*; 
public class jio0yh{ 

public static void main(String[]args){ 

FileInputStream OD=null; 

try{ 


    File f=new File("Binary.dat"); 

OD= new FileInputStream(f); 

byte[]b=new byte[(int)f.length()]; 

OD.read(b); 

for(int i=0;i<b.length;i++) 

System.out.println(b[i]);}catch(FileNotFoundException e){System.out.println 
(e.getMessage());} 

catch(IOException e){System.out.println(e.getMessage());OD.close();} 

}} 

答えて

1

OD.close();あなたのIOExceptionキャッチブロックでは、別のIOExceptionをスローする可能性もあります。

あなたは、finallyブロック内の最終OD.closeを()を囲む必要があります。

// ... Any previous try catch code 
} finally { 
    if (OD != null) { 
     try { 
      OD.close(); 
     } catch (IOException e) { 
      // ignore ... any significant errors should already have been 
      // reported via an IOException from the final flush. 
     } 
    } 
} 

より完全な説明については、以下を参照してください。

Java try/catch/finally best practices while acquiring/closing resources

関連する問題