Witajcie!
Mam sobie taką klasę i jedno pytanie względem obsługi wyjątków.
public class Connector {
private final static Logger logger = Logger.getLogger(Connector.class);
private RemoteUser remoteUser;
private Connection connection;
private Session session;
private boolean isAbleToExecuteCommands = false;
public Connector(RemoteUser remoteUser) {
logger.info("Initializing");
this.remoteUser = remoteUser;
}
public void connectToRaspberryPi() throws IOException {
createConnection();
tryToConnect();
tryToAuthenticate();
createSession();
isAbleToExecuteCommands = true;
}
private void createConnection() {
logger.info("Creating connection");
connection = new Connection(remoteUser.getRemoteHostAddress());
}
private void tryToConnect() throws IOException {
logger.info("Trying to connect to device");
connection.connect();
}
private void tryToAuthenticate() throws IOException {
logger.info("Trying to authenticate...");
if (!authenticate()) {
throw new IllegalArgumentException("The password is incorrect!");
}
}
private boolean authenticate() throws IOException {
return connection.authenticateWithPassword(
remoteUser.getRemoteUserName(),
remoteUser.getPassword());
}
private void createSession() throws IOException {
logger.info("Creating session");
session = connection.openSession();
}
public void executeCommand(String command) throws IOException {
if (isAbleToExecuteCommands) {
logger.info("Executing command:");
logger.info(command);
session.execCommand(command);
showCommandExecutionResults();
} else {
throw new IllegalStateException("The connection and session haven't been initialized!");
}
}
private void showCommandExecutionResults() throws IOException {
logger.info("Reading output...");
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
logger.info("End of output");
}
public void closeSessionAndConnection() {
try {
logger.info("Closing session");
session.close();
logger.info("Closing connection");
connection.close();
} catch (NullPointerException ex) {
throw new IllegalStateException("Connection or session hasn't been initialized!");
}
logger.info("Closed successfully");
}
protected Connection getConnection() {
return connection;
}
}
Interesuje mnie głównie metoda:
public void connectToRaspberryPi() throws IOException {
createConnection();
tryToConnect();
tryToAuthenticate();
createSession();
isAbleToExecuteCommands = true;
}
Uznałem, że skoro 3 zawarte w niej metody mogą rzucić wyjątek, to wrzucę je do jednej, która również doda do swojej sygnatury throws IOException. Zacząłem się jednak zastanawiać czy takie rozwiązanie jest dobre. Pytanie brzmi jak to logicznie ogarnąć i sprawić, żeby user wiedział, gdzie program się wysypuje?
Czy każda z metod powinna obsługiwać wyjątek IOException i rzucać inny, np. IllegalStateException z konkretną wiadomością o błędzie?
Czy metoda connectToRaspberryPi() powinna obsługiwać wszystkie metody w jednym bloku try ... catch?
Z góry dzięki za pomoc.