About Question enthuware.ocajp.i.v8.2.1347 :
Posted: Fri Sep 25, 2015 8:36 am
Hello,
I was trying out some code and I do not understand why line 2 doesn't compile but line 3 does.
If I delete line 2, line 3 will remain reachable. spit() throws a HurtException that has been caught on line 1, so any Checked exception that comes afterwards should be unreachable, in my opinion. Thanks in advance for clarifying this for me.
public class Ape {
public void run() {
try {
spit();
} catch (HurtException e) { //line 1: catches the exception thrown by spit()
System.out.println("f");
} catch (LimpException ex) { // line 2: Unreachable statement, exception has already been caught
System.out.println("e");
} catch (Exception ee) { //line 3: Compiles but should also be unreachable as the exception has
already been caught in line 1.
System.out.println("r");
}
}
public static void main(String[] args) {
new Ape().run();
}
public void spit() throws HurtException {
throw new HurtException();
}
class LimpException extends Exception {
}
class HurtException extends LimpException {
}
}
I was trying out some code and I do not understand why line 2 doesn't compile but line 3 does.
If I delete line 2, line 3 will remain reachable. spit() throws a HurtException that has been caught on line 1, so any Checked exception that comes afterwards should be unreachable, in my opinion. Thanks in advance for clarifying this for me.
public class Ape {
public void run() {
try {
spit();
} catch (HurtException e) { //line 1: catches the exception thrown by spit()
System.out.println("f");
} catch (LimpException ex) { // line 2: Unreachable statement, exception has already been caught
System.out.println("e");
} catch (Exception ee) { //line 3: Compiles but should also be unreachable as the exception has
already been caught in line 1.
System.out.println("r");
}
}
public static void main(String[] args) {
new Ape().run();
}
public void spit() throws HurtException {
throw new HurtException();
}
class LimpException extends Exception {
}
class HurtException extends LimpException {
}
}