Created
May 23, 2017 07:59
-
-
Save fermopili/b6d887548ff6df1c3bd7f8810a696a62 to your computer and use it in GitHub Desktop.
com.javarush.task.task38.task3805 Улучшения в Java 7 (multiple exceptions)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3805; | |
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3805; | |
public class ConnectionException extends Exception { | |
public ConnectionException() { | |
super(); | |
} | |
public ConnectionException(String message) { | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3805; | |
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 { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3805; | |
/* | |
Улучшения в Java 7 (multiple exceptions) | |
*/ | |
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 ( ) ); | |
//throw new SolutionException ( "WrongDataException: " + e.getMessage ( ) ); | |
} | |
// catch (ConnectionException e) | |
// { | |
// throw new SolutionException ( "ConnectionException: " + 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 ( ) ); | |
//throw new SolutionException ( "WrongDataException: " + e.getMessage ( ) ); | |
} | |
// catch (ConnectionException e) | |
// { | |
// throw new SolutionException ( "ConnectionException: " + e.getMessage ( ) ); | |
// } | |
} | |
public Object read() throws SolutionException | |
{ | |
try | |
{ | |
return connection.read ( ); | |
} | |
catch (WrongDataException | ConnectionException e) | |
{ | |
throw new SolutionException ( e.getClass ().getSimpleName ()+": " + e.getMessage ( ) ); | |
// throw new SolutionException ( "WrongDataException: " + e.getMessage ( ) ); | |
} | |
// catch (ConnectionException e) | |
// { | |
// throw new SolutionException ( "ConnectionException: " + e.getMessage ( ) ); | |
// } | |
} | |
public void disconnect() throws SolutionException | |
{ | |
try | |
{ | |
connection.disconnect ( ); | |
} | |
catch (WrongDataException | ConnectionException e) | |
{ | |
throw new SolutionException ( e.getClass ().getSimpleName ()+": " + e.getMessage ( ) ); | |
// throw new SolutionException ( "WrongDataException: " + e.getMessage ( ) ); | |
} | |
// catch (ConnectionException e) | |
// { | |
// throw new SolutionException ( "ConnectionException: " + e.getMessage ( ) ); | |
// } | |
} | |
public static void main(String[] args) throws SolutionException | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3805; | |
public class SolutionException extends Exception { | |
public SolutionException() { | |
super(); | |
} | |
public SolutionException(String message) { | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task38.task3805; | |
public class WrongDataException extends Exception { | |
public WrongDataException() { | |
super(); | |
} | |
public WrongDataException(String message) { | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task38.task3805" | |
Улучшения в Java 7 (multiple exceptions) | |
Перепиши реализации методов класса Solution. | |
Используй нововведения (multiple exceptions), касающиеся обработки исключений, которые были добавлены в Java 7. | |
Требования: | |
1. В конструкторе класса Solution в одном catch блоке должны быть обработаны исключения WrongDataException и ConnectionException. | |
2. В методе write в одном catch блоке должны быть обработаны исключения WrongDataException и ConnectionException. | |
3. В методе read в одном catch блоке должны быть обработаны исключения WrongDataException и ConnectionException. | |
4. В методе disconnect в одном catch блоке должны быть обработаны исключения WrongDataException и ConnectionException. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment