Missing exception warning

According to the API documentation the Files.copy(...) method throws the following exceptions:

  • UnsupportedOperationException

  • FileAlreadyExistsException

  • DirectoryNotEmptyException

  • IOException

  • SecurityException

We consider the following code:

static void copy(final String sourceFileName, final String destinationFileName) 
  throws FileAlreadyExistsException /* Compile time warning: Exception 
                                          is never thrown in the method */ {
  try {
    Files.copy(Paths.get(sourceFileName), Paths.get(destinationFileName));
  } catch(final IOException e) {
    System.out.println("Error copying file");

Though there is no ... catch (FileAlreadyExistsException e)... clause dealing with this checked exception we still get the indicated warning. Give an explanation.


The exception class FileAlreadyExistsException inherits from FileSystemException which in turn inherits from IOException. Thus FileAlreadyExistsException being a subclass of IOException will be caught and can thus never be thrown. This contradicts the throws declaration giving rise to a compiler warning.