It is a bit tricky. You should do quit() if you can. The problem is, if you get an IOException, then quit() may not work anyway. If you get an FTPException, quit() will generally work.
You could do something like the below:
FTPClient ftp = null;
try {
ftp = new FTPClient(...);
// do ftp stuff
}
catch (FTPException ex) {
// report
}
catch (IOException ex) {
// report
}
finally {
try {
if (ftp != null)
ftp.quit();
}
catch (IOException ex) {
// report or ignore
}
}
After some thinking -
ftp.quit(); throws IOException, so I have to try catch it if I don't want to throw it to the caller and it does not matter if I do it in finally or in first catch... Just to understand it 100% - I absolutely have to do ftp.quit() or the session will not be closed?