Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 389

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

Moderator: admin

Post Reply
javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Contact:

Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 389

Post by javiut »

In version october of 2019 page 389 in 15.3.3 just before to catch or to throw states that this code works?

English is not my nature language but for me i understand that the book states that this code works but it's not i have tested it on netbeans.

Code: Select all

class Parent{
    public static void foo()throws Exception{        
    }
}
public class EnthuwareStaticInheritedPlusExceptions extends Parent {
    public static void foo()throws Throwable{        
    }    
    public static void main(String[] args) {
    }    
}
public static void foo()throws Throwable{
}
This not works just because is throwing a Throwable and the parent or original method is throwing a exception.

But reverse it works i mean throws Throwable in parent and Exception in the child method. :twisted:

Perhaps i get it wrong :oops:

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

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 389

Post by admin »

You got it wrong. That section is above invoking a method that throws Exception and not about inheritance or overriding a method. See this sentence just before the code:
For example, assuming that the method foo throws Exception, here are valid throws clauses for a method that invokes foo:
So, if you have a method named foo that throws Exception, like this:
public static void foo() throws Exception{
if(true) throw new Exception("from foo");
}


You can call the foo method like this:
public static void bar() throws Exception{
foo();
}
OR
public static void bar() throws Throwable {
foo();
}
Complete code that you can test would be:

Code: Select all

public class TestClass {
   public static void foo() throws Exception{
     if(true) throw new Exception("from foo");
   }

   public static void bar() throws Exception{
     foo();
   }

   public static void baz() throws Throwable {
     foo();
   }

 }

javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 389

Post by javiut »

got it. Thanks another question in Enthuware planing to release a book about 816?

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests