Java Reference
In-Depth Information
IllegalChatServerException extends RuntimeException and accepts a
string as an argument to the constructor. The exception is then thrown when a specified
event occurs within the code.
class IllegalChatServerException extends RuntimeException
{
IllegalChatServerException(String message) {
super(message);
}
}
private void disconnectChatServer(Object chatServer) {
if (chatServer == null) throw new
IllegalChatServerException("Chat server is empty");
}
Solution 2
Create a class that extends java.lang.Exception to generate a checked excep-
tion class. A checked exception is required to be caught or re-thrown up the stack. In
the following example, a class identified as ConnectionUnavailableExcep-
tion extends java.lang.Exception and accepts a string as an argument to the
constructor. The checked exception is then thrown by a method in the code.
class ConnectionUnavailableException extends Exception {
ConnectionUnavailableException(String message) {
super(message);
}
}
private void sendChat(String chatMessage) throws
ConnectionUnavailableException {
if (chatServer == null)
throw new
ConnectionUnavailableException("Can't find the chat
server");
}
Search WWH ::




Custom Search