Pergunta

So basically I have a GridWorld Project that I'm doing right now in my AP Comp Sci class. I'm doing Pacman. Here is my code for the act method (for those unfamiliar with GridWorld, the act method is called every time an actor is expected to make a new move) :

public void act()
{
    Location loc = getLocation();

    if(direction==null) {
    }

    else if(direction.equals("NORTH")) {
        Location next = loc.getAdjacentLocation(loc.NORTH);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            if(getGrid().get(next) instanceof Food)
                addFood();
            moveTo(next);
            direction = "NORTH";
        }
    }

    else if(direction.equals("SOUTH")) {
        Location next = loc.getAdjacentLocation(loc.SOUTH);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            if(getGrid().get(next) instanceof Food)
                addFood();
            moveTo(getLocation().getAdjacentLocation(getLocation().SOUTH));
            direction = "SOUTH";
        }
    }

    else if(direction.equals("EAST")) {
        Location next = loc.getAdjacentLocation(loc.EAST);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            if(getGrid().get(next) instanceof Food)
                addFood();
            moveTo(getLocation().getAdjacentLocation(getLocation().EAST));
            direction = "EAST";
        }

        else if(getLocation().getCol()==20 && getLocation().getRow()==9) {
            moveTo(new Location(9,0));
            direction = "EAST";
        }
    }

    else if(direction.equals("WEST")) {
        Location next = loc.getAdjacentLocation(loc.WEST);
        if(getGrid().isValid(next) && (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
            moveTo(getLocation().getAdjacentLocation(getLocation().WEST));
            direction = "WEST";
        }

        else if(getLocation().getCol()==0 && getLocation().getRow()==9) {
            moveTo(new Location(9,20));
            direction = "WEST";
        }
    }
}

The reason for the weird wording in the last two if statements is bc I want the Pacman to be able to teleport in the real game. Now when I run the game, about 90% of the time it works but in the other 10% I get an IllegalArgumentException bc it says I am trying to move to a place that is not on the board (eg. (9,-1) and (9,21)). I want to know how I can catch or throw or whatever I need to do to stop this from happening. I have never used catch or throw so also please try to explain your reasoning thanks!

Foi útil?

Solução

To throw an exception, you use keyword throw. To catch, you use the try / catch construct. See this for more details:

For your case, you'd do something like this - this is a test case:

try {
    throw new IllegalArgumentException("Threw an IllegalArgumentException");
} catch(IllegalArgumentException e) {
    System.out.println("Caught an IllegalArgumentException..." + e.getMessage());
}

You should, however, look into your code to see why IllegalArgumentException is being thrown anyway and fix that part. Using exceptions and try / catch is for unexpected events, not events that you expect to happen and that you can handle in a better way.

For example, FileNotFoundException gets thrown when a file could not be found. You generally try / catch that, so that you do something if the file was not found. However, if it's expected in a reasonable number of cases that the file might not be there, it would be better to first check if the file exists and then if it does actually do something with it.

Outras dicas

You can catch IllegalArgumentException in the same way you catch a normal exception. Typically an IAE comes from a state that invalidates your program, so if you receive a negative index you will need to convert that to its equivalent valid value. In terms of how your program is intended to function, typically a negative index reference is not a good thing.

It seems you want to be able to catch your exceptions to keep your game running, and you are contented with parameters being wrong. So effectively you are using exceptions into the logic of the program, defeating their purpose.

An IllegalArgumentException is a runtime exception, you should not catch them and the program should just fail in some way. Simply make sure that the parameters passed are correct.

You can learn to catch and throw exceptions easily with one of the many tutorials found on Google.

However, and IllegalArgumentException means that some parameter you're sending to a third-party API is wrong. You should debug your application and see what code is causing the trouble, check the API documentation to see the requirements/constraints of the parameters you are breaking and fix it.

Of course, you can only add a catch (IllegalArgunentException e) and do nothing about it. Your program may run, but maybe it can crash later.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top