NoSuchFileException — 파일 없음 예외

NoSuchFileException — 파일 없음 예외

존재하지 않는 파일에 접근하려고 할 때 던져지는 checked 예외예요. Files 클래스의 파일 연산 메서드들이 대상 파일이 없을 때 이 예외를 던져요. java.io.FileNotFoundException과 달리 이 예외는 IOException의 하위 타입이에요.

출처: Java API Reference

본문

  • public NoSuchFileException(String file) — 지정된 파일의 인스턴스를 생성해요.
  • public NoSuchFileException(String file, String other, String reason) — 지정된 파일(file), 다른 파일(other), 그리고 실패 이유(reason)로 인스턴스를 생성해요.

이 클래스는 FileSystemException을 상속하며, FileSystemException이 제공하는 getFile(), getOther(), getReason() 같은 메서드를 그대로 사용할 수 있어요. 보통 IOException으로 잡은 뒤 e instanceof NoSuchFileException으로 구분하거나, 특정 파일 연산(Files.copy, Files.delete, Files.move 등)에서 대상 파일이 없으면 이 예외를 잡아 처리해요.

try {{
    Files.delete(nonexistent);
}} catch (NoSuchFileException e) {{
    System.err.println("파일 없음: " + e.getFile());
}}

더 알아보기 (Learn more)