From f5074ba9b026c99d5744e6c9cb2b8bf1d0784df8 Mon Sep 17 00:00:00 2001 From: NoxArt Date: Tue, 19 Nov 2013 18:38:11 +0100 Subject: [PATCH] Improved exception handling, issue #9 (does not solve) --- src/cz/fit/tam/ConnectToGameActivity.java | 9 ++++--- src/cz/fit/tam/WaitForGameActivity.java | 5 +++- src/cz/fit/tam/model/Game.java | 30 ++++++++++++++--------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/cz/fit/tam/ConnectToGameActivity.java b/src/cz/fit/tam/ConnectToGameActivity.java index 8273c1d..e643316 100644 --- a/src/cz/fit/tam/ConnectToGameActivity.java +++ b/src/cz/fit/tam/ConnectToGameActivity.java @@ -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; } diff --git a/src/cz/fit/tam/WaitForGameActivity.java b/src/cz/fit/tam/WaitForGameActivity.java index 545b87d..f63b11b 100644 --- a/src/cz/fit/tam/WaitForGameActivity.java +++ b/src/cz/fit/tam/WaitForGameActivity.java @@ -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; /* @@ -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(); } diff --git a/src/cz/fit/tam/model/Game.java b/src/cz/fit/tam/model/Game.java index e04a45f..8296831 100644 --- a/src/cz/fit/tam/model/Game.java +++ b/src/cz/fit/tam/model/Game.java @@ -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 { @@ -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; @@ -68,7 +71,7 @@ public List getChatMessages() { public void create() { if (isConnected()) { - throw new IllegalStateException(); + throw new AlreadyConnectedException(); } client.createGame(properties); @@ -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()) { @@ -104,18 +107,21 @@ public List 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()); @@ -124,7 +130,7 @@ public void leave() { public void sendWords(Integer round, String[] words) { if (isConnected()) { - throw new IllegalStateException(); + throw new NotConnectedException(); } if (isStopped()) {