How can I throw and catch an IllegalArgumentException?

So basically I have a GridWorld project that I am doing right now in the AP Comp Sci class. I am doing Pacman. Here is my code for the action method (for those unfamiliar with GridWorld, the act method is called every time the actor has 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 strange formulation in the last two statements is bc. I want Pacman to be able to teleport to a real game. Now when I start the game, it works in about 90% of cases, but in the other 10% I get IllegalArgumentException bc, it says I'm trying to go to a place that is not on the board (for example, (9, -1) and (9.21)). I want to know how I can catch or throw or what I need to do to prevent this from happening. I never used a catch or toss, so also try to explain your gratitude for the reasoning!

+4
source share
4 answers

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

In your case, you would do something like this - this is a test example:

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

However, you should look into your code to find out why an IllegalArgumentException is thrown and corrects this part anyway. Using exceptions and try / catch for unexpected events, not the events you expect and which you can handle better.

For example, a FileNotFoundException gets thrown when a file cannot be found. You usually try / catch this, so you do something if the file is not found. However, if in a reasonable number of cases it is expected that the file may be missing, it would be better to first check if the file exists, and then if it really does something with it.

+5
source

You can catch an IllegalArgumentException just like you will catch a normal exception. Typically, the IAE comes from a state that invalidates your program, so if you get a negative index, you will need to convert it to an equivalent valid value. In terms of how your program is designed to work, usually a negative index reference is not good.

0
source

It seems you want to catch your exceptions to make your game work, and you are happy with the wrong parameters. Thus, you use exceptions in the program logic, defeating their purpose.

An IllegalArgumentException is a run-time exception, you must not catch them, and the program must somehow fail. Just make sure the parameters passed are correct.

0
source

You can learn how to easily catch and throw exceptions using one of the many guides found on Google.

However, IllegalArgumentException also means that the parameter that you send to the third-party API is incorrect. You should debug your application and see which code is causing the problem, check the API documentation to see the requirements / restrictions of the parameters that you violate, and fix them.

Of course, you can only add catch (IllegalArgunentException e) and do nothing. Your program may work, but perhaps it may crash later.

0
source

Source: https://habr.com/ru/post/1397508/


All Articles