Suppose in Java, I’m using a preexisting interface that is rather general
public interface Generator {
public String generate();
}
and I have my own class
public class FromFileGenerator implements Generator {
...
public String generate() throws FileNotFoundException {
String output = //read from some file
return file;
}
}
The Java compiler yells at me because the implementation of generate() contains an exception not specified in the original signature (FileNotFoundException). However, clearly the exception does not belong in the interface, but it also cannot be neglected in the implementing class. How can this be resolved without simply failing silently?
You can wrap the implementation exception in an unchecked exception and throw that: