0

This question can be seen as a whole in here. Though the linked post explains this issue, you should not think of it as a duplicate: it asks a different question and has an accepted answer for this different question.

Though the question can be read in the other post, I'll explain it here as well:

I'm developing a snake-like game that can be played with AIs, and is intended for that purpose. All such AIs should extend an abstract class called SnakeLogic. All such AIs should also reside in their standalone .jar archives in a specific folder, from where the main program can find them and list them using classloaders.

The user can then choose one of his/her AIs from a list, should all stars fall in line, and play a game with this AI.

Now, I have a method in my main program that gets the next move from the AI like so:

public void startGame(int speed) {        
    gameInterface.showWindow();
    Runnable moveCmd = () -> {
        try {

            for (Player player : snakeGame.getPlayers()) {
                if (player.isDead()) {
                    continue;
                }

                String move = player.getLogicHandler().getMove();

                Direction direction = Direction.directionFromString(move);
                snakeGame.makeMove(player, direction);
            }

            gameInterface.getFrame().repaint();
            snakeGame.wait(speed);

            if (snakeGame.gameOver()) {
                stopGame();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            stopGame();
        }
    };

    /* moveSchedule is an instance of ScheduledExecutorService */
    moveSchedule.scheduleAtFixedRate(moveCmd, 1000, speed, TimeUnit.MILLISECONDS);        
}

I'm not going to get too involved with the code above. I'd like to draw your attention to the try-catch statement, however. As you can see I print the stacktrace and end the game, should an exception occur somewhere during the execution of the moveCmd runnable. If I don't print the stacktrace like this, or if I remove the try-catch entirely, I never get any errors in the case of a runtime exception during the execution of that block. Why? Is it because it's wrapped inside a runnable? Note also that the line snakeGame.makeMove(player, direction); doesn't call any code in the main program; snakeGame is an instance of a SnakeLogic, which resides in an external .jar.

Why don't I get any errors if I remove the try-catch? By errors I mean runtime errors.

Community
  • 1
  • 1
Olavi Mustanoja
  • 2,045
  • 2
  • 23
  • 34

1 Answers1

0

Because of this code in FutureTask:

 try {
            runner = Thread.currentThread();
            if (getState() == RUNNING)
                callable.call(); // don't set result
            runner = null;
            return compareAndSetState(RUNNING, 0);
        } catch (Throwable ex) {
            innerSetException(ex);
            return false;
        }

So it is catching the exception and setting it to return only on performing Callable.get()

AdrianS
  • 1,980
  • 7
  • 33
  • 51
  • Is there a way of aquiring the exception, or telling it to throw it like a normal good-behaving program, apart from how I did it? – Olavi Mustanoja Dec 08 '14 at 08:38
  • You can either surround as you did, everything with a Try-Catch in the run() method or use a Callable which can throw exceptions: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html – AdrianS Dec 08 '14 at 08:43