About Question enthuware.ocajp.i.v7.2.1006

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

Moderator: admin

Post Reply
sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

About Question enthuware.ocajp.i.v7.2.1006

Post by sarakh »

If we compare this question to enthuware.ocajp.i.v7.2.1276

enthuware.ocajp.i.v7.2.1276:

Code: Select all

public class TestClass{
   public static void main(String[] args){
      TestClass tc = new TestClass();
      try{
         tc.m1();
      }
      catch (MyException e){
         tc.m1();
      }
      finally{
         tc.m2();
      }
   }
   public void m1() throws MyException{
      throw new MyException();
   }
   public void m2() throws RuntimeException{
      throw new NullPointerException();
   }
}
enthuware.ocajp.i.v7.2.1006:

Code: Select all

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
   public static void main(String [] args) throws Exception{
      try{
         m2();
      }
      finally{ m3(); }
    }
    public static void m2() throws NewException{  throw new NewException();  }
    public static void m3() throws AnotherException{  throw new AnotherException();  }
}
We see that
in enthuware.ocajp.i.v7.2.1276, the catch clause is using a method which is throwing a checked exception.
in enthuware.ocajp.i.v7.2.1006 the finally clause is using a method which is throwing a checked exception.

in enthuware.ocajp.i.v7.2.1276, we said: "It will not compile because of unhandled exception", so if the catch clause is using a method which is throwing a checked exception, this exception should be handled.

Why is it that we don't need to handle the checked exception thrown by the finally clause in enthuware.ocajp.i.v7.2.1006??

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

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by admin »

Because there is a throws clause in main.
If you like our products and services, please help us by posting your review here.

Marthinus
Posts: 7
Joined: Tue Jul 22, 2014 5:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by Marthinus »

If I copy and paste the code in the question and I run javac on it, it compiles?

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
public static void main(String [] args) throws Exception{
try{
m2();
}
finally{ m3(); }
}
public static void m2() throws NewException{ throw new NewException(); }
public static void m3() throws AnotherException{ throw new AnotherException(); }
}

Marthinus
Posts: 7
Joined: Tue Jul 22, 2014 5:07 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by Marthinus »

Nevermind that. That is what is expected :|

chandu1987
Posts: 4
Joined: Wed Dec 24, 2014 11:41 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by chandu1987 »

Paul, I read somewhere that if a code inside a try block is throwing a checked exception, then the try block should always be followed by catch block. Can you clarify on this please?

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

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by admin »

No, that is not correct. It may just have a finally or it may have a catch block that doesn't catch that exception and the method has a throws clause. Please try writing some test code to try out the possibilities.
If you like our products and services, please help us by posting your review here.

chandu1987
Posts: 4
Joined: Wed Dec 24, 2014 11:41 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by chandu1987 »

admin wrote:No, that is not correct. It may just have a finally or it may have a catch block that doesn't catch that exception and the method has a throws clause. Please try writing some test code to try out the possibilities.
Ok got it, Thank you.

meghajor
Posts: 7
Joined: Fri Nov 20, 2015 7:03 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by meghajor »

hi
Why does it say in the note newException thrown in the try block is ignored because of AnotherException in finally?
I understand finally will be executed no matter what.Why it doesnt print both the Exceotions?.



"Since finally throw AnotherException (due to a call to m3() ), the NewException thrown in the try block ( due to call to m2() ) is ignored and AnotherException is thrown from the main method."

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

Re: About Question enthuware.ocajp.i.v7.2.1006

Post by admin »

Because that is how the language is designed. The caller of the method will not get NewException. It will get AnotherException. An exception thrown by code within finally block basically overrides the exception thrown by code in the try block.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 113 guests