Can somebody explain to me why the following code is OK for java compiler:
private InputStream getResourceAsStream(String resourceName) {
return getClass().getClassLoader().getResourceAsStream(resourceName);
}
private byte[] readResourceToByteArray(String resourceName) {
try (InputStream in = getResourceAsStream(resourceName)) {
return IOUtils.toByteArray(in);
} catch (IOException e) {
throw new AssertionFailedError();
}
}
But in the following one:
private InputStream getResourceAsStream(String resourceName) {
return getClass().getClassLoader().getResourceAsStream(resourceName);
}
private byte[] readResourceToByteArray(String resourceName) {
try (InputStream in = getResourceAsStream(resourceName)) {
return IOUtils.toByteArray(in);
} catch (IOException e) {
throwException();
}
}
private void throwException() {
throw new AssertionFailedError();
}
I'm getting missing return statement java compilation error in method readResourceToByteArray() if it is clear that in catch block I'm throwing exception so no return should be needed ?