I am parsing a text file (UTF-8) and checking for several conditions that need to be satisfied for the code to be happy. If not, then there is no point to continue and the program should die.
At the moment I am catching exceptions and if caught, throw RuntimeException
to signal the user that something went wrong.
What would be a proper way of doing this or is it OK the way it is?
/**
* Reads the maze from plain text and converts it to char[][]
* Marks the start (B) and end (F) coordinates if found, if not throws exception
* Expects each line to have the same length as the the first line, throws exception if not
*
* @return maze as char[][]
*/
private char[][] readMazeFromFile() {
List<String> stringList;
try {
stringList = Files.readAllLines(Paths.get("h40.in"), Charset.forName("UTF-8"));
} catch (MalformedInputException e){
throw new RuntimeException(String.format("Did not understand file,
check the charset, expecting UTF-8\n%s", e.getMessage()));
} catch (IOException e) {
throw new RuntimeException(String.format("Did not understand file,
sorry\n%s", e.getMessage()));
} catch (Exception e){
throw new RuntimeException(String.format
("Generic error while reading the input file, sorry\n%s", e));
}
char[][] charMaze = new char[stringList.size()][];
for (int i = 0; i < stringList.size(); i++) {
if (stringList.get(i).contains("B")) {
startX = i;
startY = stringList.get(i).indexOf("B");
}
if (stringList.get(i).contains("F")) {
endX = i;
endY = stringList.get(i).indexOf("F");
}
try {
charMaze[i] = stringList.get(i).toCharArray();
} catch (ArrayIndexOutOfBoundsException e) {
throw new RuntimeException(String.format("Did not understand the maze,
expecting square.\n%s", e.getMessage()));
}
}
if (startX == -1 || startY == -1)
throw new RuntimeException("Could not find starting point (B), aborting.");
if (endX == -1 || endY == -1)
throw new RuntimeException("Could not find ending point (F), aborting.");
return charMaze;
}