I'm trying to create a method f1(x)
that throws an exception when x equals 5. After that I will try to call that method from another method f2()
to invoke that exception. Then I have to have f2()
recover by calling f1(x+1)
. I tried coding something, but I'm stuck. Here is the code:
public class FiveException extends Exception {
public void f1(int x) throws FiveException {
if (x == 5) {
throw new FiveException();
}
}
public void f2() {
int x = 5;
try {
f1(x);
}catch (FiveException e) {
System.out.println("x is 5");
}
}
public static void main(String[] args) {
FiveException x5 = new FiveException();
x5.f2();
}
}
The print statement works, but I'm not sure how to call f(x+1)
. Any help on how to fix this and any techniques to write exceptions is appreciated.