Page 1 of 1

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

Posted: Fri Mar 06, 2020 1:42 pm
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:

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

Posted: Fri Mar 06, 2020 10:33 pm
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();
   }

 }

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

Posted: Sat Mar 07, 2020 6:47 am
by javiut
got it. Thanks another question in Enthuware planing to release a book about 816?