1

Basically i'm writing a game in Java where i want the program to tell the user that he can't move right or left if that move will cause the player to move out of the array, which means this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Game.printmap(Game.java:58)
at Game.main(Game.java:17)

The Array is the map where the character can move and i want to avoid it from going outside of it. It is the game world where the player can move inside.

Is there a way to make my program ask for input again if the player choice implies an OutOfBounds error?

Thank you

  • related (possibly a duplicate): [How much information about an error should be shown to the user?](http://programmers.stackexchange.com/questions/245255/how-much-information-about-an-error-should-be-shown-to-the-user) – gnat Jan 14 '15 at 16:48

1 Answers1

3

Well, the best way to deal with this is to fix the error that caused the exception to be thrown in the first place.

That said, you could always write a top-level try catch block. This will catch any exceptions that weren't handled elsewhere.

public static void main(String args[])
{
    while (userDidNotExit)
    {
        try
        {
            // execute entry point of program here.
        }
        catch (Exception e)
        {
            // perform some sort of recovery here.
            // 
        }
    }
}
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • In this case, he should really be checking closer to the array access. The rule is throw early, catch late. Throw exceptions as soon as you know something is wrong. Only handle them once it makes sense and you can do something reasonable with the exception. Sometimes, that's at the global level. Usually, that's somewhere that a user can make a decision. – mgw854 Jan 14 '15 at 00:48
  • Of course. But the question was how to prevent the application from crashing when it throws an exception. – Robert Harvey Jan 14 '15 at 00:57
  • To explain the situation further: Let's say that you're in the cell number 0, so you can't move left. When asked, the player decides to go left and this causes the OutOfBounds error and crash. I'd love to find a way to prompt again the question so that the player can answer again, this time choosing "forward" or "right" instead. I don't know how to use Try Catch, i'll document myself and try it out. Thank you! – Filipe Madureira Jan 14 '15 at 01:05
  • 3
    @FilipeMadureira: The way to avoid the OutOfBounds exception is by testing (with `if` statements) if the move is legal for the game and to show an error message if the move is illegal instead of making the move. – Bart van Ingen Schenau Jan 14 '15 at 09:15
  • @BartvanIngenSchenau Thank you! It was THAT simple! I added some IF statements when the Character.moving is triggered. One moment prior to actually moving i check if the X variable in the grid (from 0 to 9, 10 rows in total) is 0 or 9 thus avoiding moving when an error would be displayed and instead i print some warning message asking the player to choose a different direction to move to. – Filipe Madureira Jan 14 '15 at 13:18