Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1347 :

Posted: Fri Sep 25, 2015 8:36 am
by heleneshaikh
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 {
}
}

Re: About Question enthuware.ocajp.i.v8.2.1347 :

Posted: Fri Sep 25, 2015 8:50 am
by admin
What if the call to spit() throws a RuntimeException? Where will it go?

Re: About Question enthuware.ocajp.i.v8.2.1347 :

Posted: Fri Sep 25, 2015 9:08 am
by heleneshaikh
Got it, thanks!

Re: About Question enthuware.ocajp.i.v8.2.1347 :

Posted: Sat May 26, 2018 3:55 am
by mihhay
Why/what is different when split() throws a RuntimeException ?

Re: About Question enthuware.ocajp.i.v8.2.1347 :

Posted: Mon May 28, 2018 9:22 pm
by admin
If no one catches it (as is the case here), it will go to the JVM and the JVM will fibally catch it and kill the thread that was ececuting this code.

Not required fpr the exam but here is a link that talks about it if you are interested How uncaught exceptions are handled in Java - https://www.javamex.com/tutorials/excep ... dler.shtml