Skip to content

Instantly share code, notes, and snippets.

@ZhdanRuslan
Created September 11, 2016 12:01
Show Gist options
  • Save ZhdanRuslan/76a337d3d78ef370fa74f4a4d8e23192 to your computer and use it in GitHub Desktop.
Save ZhdanRuslan/76a337d3d78ef370fa74f4a4d8e23192 to your computer and use it in GitHub Desktop.
Level 38, Lesson 06, Home 02
package com.javarush.test.level38.lesson06.home02;
public interface Connection {
void connect() throws WrongDataException, ConnectionException;
void write(Object data) throws WrongDataException, ConnectionException;
Object read() throws WrongDataException, ConnectionException;
void disconnect() throws WrongDataException, ConnectionException;
}
package com.javarush.test.level38.lesson06.home02;
public class ConnectionException extends Exception {
public ConnectionException() {
super();
}
public ConnectionException(String message) {
super(message);
}
}
package com.javarush.test.level38.lesson06.home02;
public class ConnectionMock implements Connection {
@Override
public void connect() throws WrongDataException, ConnectionException {
}
@Override
public void write(Object data) throws WrongDataException, ConnectionException {
}
@Override
public Object read() throws WrongDataException, ConnectionException {
return null;
}
@Override
public void disconnect() throws WrongDataException, ConnectionException {
}
}
package com.javarush.test.level38.lesson06.home02;
/* Улучшения в Java 7 (multiple exceptions)
Перепиши реализации методов класса Solution.
Используй нововведения, касающиеся обработки исключений, которые были добавлены в Java 7.
*/
public class Solution {
private final Connection connection;
public Solution() throws SolutionException {
try {
connection = new ConnectionMock();
connection.connect();
}
catch (WrongDataException | ConnectionException e) {
throw new SolutionException(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
public void write(Object data) throws SolutionException {
try {
connection.write(data);
}
catch (WrongDataException | ConnectionException e) {
throw new SolutionException(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
public Object read() throws SolutionException {
try {
return connection.read();
}
catch (WrongDataException | ConnectionException e) {
throw new SolutionException(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
public void disconnect() throws SolutionException {
try {
connection.disconnect();
}
catch (WrongDataException | ConnectionException e) {
throw new SolutionException(e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
}
package com.javarush.test.level38.lesson06.home02;
public class SolutionException extends Exception {
public SolutionException() {
super();
}
public SolutionException(String message) {
super(message);
}
}
package com.javarush.test.level38.lesson06.home02;
public class WrongDataException extends Exception {
public WrongDataException() {
super();
}
public WrongDataException(String message) {
super(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment