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 {
}
}
About Question enthuware.ocajp.i.v8.2.1347 :
Moderator: admin
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1347 :
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.
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1347 :
Got it, thanks!
-
- Posts: 10
- Joined: Wed May 09, 2018 12:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1347 :
Why/what is different when split() throws a RuntimeException ?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1347 :
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
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.
Who is online
Users browsing this forum: No registered users and 115 guests