2016-04-05 25 views
0

Atılması gerektiği veya yakalanacağı bildirilmelidir Bir sorum var, neden java bu istisnayı atmaya devam ediyor! Sorun dere ile mi? çünkü tüm IOException'ları elimden aldım!bildirilmemiş istisna IOException;

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

Bu benim elde ettiğim istisna! Burada

benim kodudur

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

}} 

cevap

1

OD.close(); IOException catch bloğunuzda başka bir IOException atmak da hassastır.

Bir nihayet bloğunda son OD.close() çevrelemelisiniz:

// ... 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. 
     } 
    } 
} 

daha kapsamlı bir açıklama için aşağıdaki bakın:

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