2017-11-23 12 views
1

"보고되지 않은 예외 ioexception; 오류가 계속 발생하거나 보관해야하거나 throw되어야한다고 선언했습니다." 제공된 경로 또는 try-catch 블록의 오류입니다."보고되지 않은 예외 ioexception이 발생했습니다. 보관해야하거나 throw해야한다고 선언했습니다."

import java.io.*; 
import java.util.regex.*; 

class RegexMobileExtractor { 

    public static void main(String[] args) { 
     try { 
      Pattern p = Pattern.compile("(0|9)?[7-9][0-9]{9}"); 
      PrintWriter pw = new PrintWriter("C:\\Users\\HP\\Desktop\\CODE\\JAVA_EX\\copy\\output.txt"); 
      BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\CODE\\JAVA_EX\\copy\\input.txt")); 

      //PrintWriter pw = new PrintWriter("output.txt"); 
      //BufferedReader br = new BufferedReader(new FileReader("input.txt")); 

      String line = br.readLine(); 
      while(line!= null) { 
       Matcher m = p.matcher(line); 
       while(m.find()) { 
        pw.println(m.group()); 
       } 

       line = br.readLine(); 
      } 

      pw.flush(); 
      pw.close(); 
      //br.close(); 
     } catch (FileNotFoundException obj) { 
      System.out.println("errr occured"); 
     } 
    } 
} 
+2

그래서 'IOException'을 포착하면 여러 개의 캐치가있을 수 있습니다. –

답변

1

코드 행 : br.readLine();IOException을 던질 수 있습니다. IOException 이후

} catch (IOException obj) { 
    System.out.println("errr occured"); 
} 

가있다

} catch (FileNotFoundException obj) { 
    System.out.println("errr occured"); 
} 

catch (IOException e) { 
    e.printStackTrace(); 
} 
0

변경 라인 :이 chacked exception 및 컴파일러 힘 당신이 그것을 처리, 즉 추가 catch 블록을 추가해야 할 이유 부모 FileNotFoundException 클래스의 경우 두 가지 예외를 모두 처리합니다.

또한 파일을 자동으로 닫을 try-with-resources을 사용하는 것이 좋습니다.