Exceptions are basically nothing but names. My best advice here is to make your own. The main reason is that IllegalArgumentException are thrown exceptions because they extend java.lang.RuntimeException . They will simply cause problems if you have used them in this context. (A source)
Change the method signature to
private char[][] readMazeFromFile(Path mazeFile) throws IOException, MazeParseException {...}
And all throw new IllegalArgumentException with throw new MazeParseException (except for the first use as per @Joel's answer)
MazeParseException.java file:
package yourPackage.here; import java.lang.Exception; public class MazeParseException { public MazeParseException() { super(); } public MazeParseException(String reason) { super(reason); } }
The advantages of using your own exception are that you can mark additional data along with an exception related to your situation, for example, you can add:
private int errorLineNum = null; public MazeParseException(String reason, int lineNum) { this(reason); this.errorLineNum = lineNum; } public int getMalformedLine() { return this.errorLineNum; }
source share