2
I was making corrections in a class and I came across the following code, I didn’t know it was possible and I never stopped to think, but why is this valid? I mean first line of the Try. For me Try/catch always been in the format:
try{
...
}catch(Exception e){
...
}
The following code is the same as writing in the "syntax" above?
try (FileInputStream fs = new FileInputStream(templateFile)) {
Workbook wb = write(validation, WorkbookFactory.create(fs), planilha);
File file = relatorioPlanilhaDAO.exportSheet(planilha.getNomeHash());
FileOutputStream fout = new FileOutputStream(file);
if (wb != null) {
wb.write(fout);
} else {
Log.error(this, "Erro ao escrever no arquivo.");
throw new InternalServerErrorException("Erro ao exportar relatório.");
}
fout.flush();
fout.close();
return file;
} catch (IOException e) {
Log.info(this, "Erro ao obter planilha.", e);
throw new NotFoundException("Erro ao exportar planilha.", e);
} catch (InvalidFormatException | IllegalArgumentException e) {
Log.error(this, "Formato de planilha inválido.", e);
throw new InternalServerErrorException("Formato de planilha inválido.", e);
}
see this issue @Danielamarquesdemora http://answall.com/questions/71638/como-n%C3%A3o-include-o-Finally-e-yet-yes-close-io-streams-Connections-statement
– Filipe Miranda