Skip to content

Commit

Permalink
Improved exception handling, issue #9 (does not solve)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoxArt committed Nov 19, 2013
1 parent 26eba47 commit f5074ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/cz/fit/tam/ConnectToGameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,13 @@ protected Boolean doInBackground(ConnectToGameActivity... wrapper) {
try {
connectActivity.getSelectedGame().connect(
connectActivity.getSelectedGameId());
} catch( Game.AlreadyConnectedException e ) {
Log.e("ERROR", "Already connected");
} catch (Exception e) {
Log.e("ERROR", e.getMessage());
Toast.makeText(connectActivity, "ERROR " + e.getMessage(),
Toast.LENGTH_SHORT).show();
Log.e("ERROR", e.getClass().getName());

/*Toast.makeText(connectActivity, "ERROR " + e.getMessage(),
Toast.LENGTH_SHORT).show();*/
}
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/cz/fit/tam/WaitForGameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.TextView;
import android.widget.Toast;
import cz.fit.tam.model.Game;
import cz.fit.tam.model.GameClient;
import cz.fit.tam.model.GameProperties;

/*
Expand Down Expand Up @@ -79,7 +80,9 @@ protected Boolean doInBackground(WaitForGameActivity... activity) {
activityWait = activity[0];
try {
activity[0].getCurrentGame().leave();
} catch (Exception e) {
} catch(Game.NotConnectedException e) {
Log.e("ERROR", "Leaving when not connected");
} catch (Exception e) {
Toast.makeText(activity[0], "ERROR " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
Expand Down
30 changes: 18 additions & 12 deletions src/cz/fit/tam/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.util.Log;

public class Game implements Serializable {

Expand All @@ -16,8 +14,13 @@ public class Game implements Serializable {
*/
private static final long serialVersionUID = 1143771934418399254L;

public class GameIsStoppedException extends RuntimeException {
}
public class GameIsStoppedException extends RuntimeException {}

public class NotConnectedException extends RuntimeException {}

public class AlreadyConnectedException extends RuntimeException {}

public class NotAdminException extends RuntimeException {}

private GameProperties properties;

Expand Down Expand Up @@ -68,7 +71,7 @@ public List<ChatMessage> getChatMessages() {

public void create() {
if (isConnected()) {
throw new IllegalStateException();
throw new AlreadyConnectedException();
}

client.createGame(properties);
Expand All @@ -77,7 +80,7 @@ public void create() {

public void connect(Integer id) {
if (isConnected() || this.getProperties().getId() != null) {
throw new IllegalStateException();
throw new AlreadyConnectedException();
}

if (isStopped()) {
Expand All @@ -104,18 +107,21 @@ public List<GameProperties> getGames() {
}

public void stop() {

if ((isConnected() == false) || (isAdmin() == false)) {
throw new IllegalStateException();
if (isConnected() == false) {
throw new NotConnectedException();
}

if (isAdmin() == false) {
throw new NotAdminException();
}

client.stop(properties.getId());
stopped = true;
}

public void leave() {
if (isConnected()) {
throw new IllegalStateException();
if (isConnected() == false) {
throw new NotConnectedException();
}

client.leave(properties.getId());
Expand All @@ -124,7 +130,7 @@ public void leave() {

public void sendWords(Integer round, String[] words) {
if (isConnected()) {
throw new IllegalStateException();
throw new NotConnectedException();
}

if (isStopped()) {
Expand Down

0 comments on commit f5074ba

Please sign in to comment.