例6-4
public void connectMe(String servename) throws ServerTimedOutException {
int sucess;
int portToConnect = 80;
success = open(serveName,portToConnect);
if (success == -1) {
throw new ServerTimedOutException();
}
}
使用try语句可捕获该异常:
public void findServer (){
...
try {
connectMe(defaultServer);
} catch(ServerTimedOutException e) {
g.drawString("Server timed out,trying alternate",5,5);
try {
connectMe(alternateServer);
}catch(ServerTimedOutException e1){
g.drawString("No server currently available",5,5);
}
}
...
}