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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post 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 {
}
}

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What if the call to spit() throws a RuntimeException? Where will it go?
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post by heleneshaikh »

Got it, thanks!

mihhay
Posts: 10
Joined: Wed May 09, 2018 12:48 pm
Contact:

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

Post by mihhay »

Why/what is different when split() throws a RuntimeException ?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 84 guests