I need to throw a couple times a custom exception. A test class is checking if my solution works but I’m not allowed to make any changes to this class which leads me to my problem. I simplified the problem here, because the whole code is not needed here
public class Test{ public static final String s = "0test"; @Test public void testZero(){ Solver sol = new Solver(Parser.run(s)); //IntelliJ is underlining "run" because "Unhandled exception: ParseException", a //simple solution could be adding "throws ParseException" in the head, but I'm not //allowed to change the test class } } public class Parser{ public static Pars run(String input) throws ParseException{ if(input.charAt(0) == '0'){ throw new ParseException("..."); } } } public class ParseException extends Exception{ public ParseException(String mess){ super(mess); } }
Advertisement
Answer
I have found the solution for my problem. In my ParseException class, I need to change it to:
public class ParseException extends IllegalArgumentException{...}
Further into the task, the test class didnt accept my exception because there was a IllegalArgumentException exspected. Changing it into “extends IllegalArgumentException” solves the problem, so I dont need “throws ParseException” in the headings and no try,catch statements