title: How do I throw and catch an IllegalArgumentException?
body: <p>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) :</p>
  
  <pre><code>public void act()
  {
      Location loc = getLocation();
  
      if(direction==null) {
      }
  
      else if(direction.equals("NORTH")) {
          Location next = loc.getAdjacentLocation(loc.NORTH);
          if(getGrid().isValid(next) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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 &amp;&amp; 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) &amp;&amp; (getGrid().get(next)==null || getGrid().get(next) instanceof Food)) {
              moveTo(getLocation().getAdjacentLocation(getLocation().WEST));
              direction = "WEST";
          }
  
          else if(getLocation().getCol()==0 &amp;&amp; getLocation().getRow()==9) {
              moveTo(new Location(9,20));
              direction = "WEST";
          }
      }
  }
  </code></pre>
  
  <p>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!</p>