About Question enthuware.ocajp.i.v7.2.1276 :

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

Moderator: admin

Post Reply
icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

About Question enthuware.ocajp.i.v7.2.1276 :

Post by icepeanuts »

I just didn't know the Catch or Specify Requirement also applies to the catch and finally blocks. I usually focused on the try block and ignored the rest blocks. Thanks for reminding me on this.

Guest

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

Post by Guest »

i tried this program, it compiled just fine, it should have been option (E).

Come on guys, you're making more nervous now!!

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

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

Post by admin »

Are you sure you tried the program exactly as given? I just tried it and it doesn't compile because of the reason given in the explanation.
-Paul.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

admin wrote:Are you sure you tried the program exactly as given? I just tried it and it doesn't compile because of the reason given in the explanation.
-Paul.
My bad Paul, I was modifying a previous exercise and i forgot to delete "throws Exception" from the main method signature....

M.Komarov
Posts: 17
Joined: Fri Sep 06, 2013 1:37 am
Contact:

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

Post by M.Komarov »

The explanation in the test seems to be obvious, but… I decided to make one small change:

Code: Select all

finally {
    throw new NullPointerException();
    //tc.m2();
}
The only change is that I moved the generation of NullPointerException from m2() directly to finally{}. This code will compile! Why? Can the exception in finally block "shadows" for the compiler exception in catch block?

UPD: Good explanation of the interaction of exceptions in catch-finally is given inside test question QID=enthuware.ocajp.i.v7.2.1210: "The Exception that is thrown in the last, is the Exception that is thrown by the method to the caller… even if the catch blocks throws some exception, the control goes to finally… Other exceptions thrown by the code prior to this point are lost."

My question is more related to the behavior of the compiler – I can't catch the logic.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

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

Post by coder007 »

There is not possibility to handle a checked exception thrown by catch block (or if its not handled by any catch block) in its respective finally block, right?

We can only use nested try-block inside that catch, like this:

Code: Select all

try{
         test.m1();
      }

      catch (MyException e1)
      {
         try 
         {
           test.m1();
         }
         catch (MyException e2) {}
         
      }

      finally
      {
        //test.m2();
      }

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

magdute70
Posts: 4
Joined: Fri Mar 20, 2015 4:34 pm
Contact:

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

Post by magdute70 »

I think I understood reason :lol: : m2() may throw RuntimeException, may not, this why there is no quarantee that exception, thrown by m2() will overwrite (and, hence, remove) MyException thrown by m1(). If m2() will not throw, MyException will propagate unhandled. This is not case when generating RuntimeException directly in finally block - in last case compiler knows for sure MyException will allways be overwritten and compiles happily :D .

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM »

Hi Paul,

I wanted to ask what happens actually if the catch block throws an exception too.
If the code is like this:

public class E1 extends Exception {}
public class E2 extends Exception{}

public void methodX() {

try {
//throw Exception E1
}
catch (Exception E1) {
// throw Exception E2
}
catch (Exception E2) {
// catch E2
}

}

Will E2 be catched by the following catch block after being thrown inside the first catch block ? I assume not. I think E2 has to be either caught inside the first catch block or declared in the throws section of the method it belongs to?
Did I understand this right?

Thanks for checking this out.

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

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

Post by admin »

You need to compile and execute the code and see what happens. Let me know if you have trouble understanding the output.
-Paul.
If you like our products and services, please help us by posting your review here.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM »

Hi Paul,

Understood what happened. Thanks

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post by JaredTse »

Can someone please shade some light as to why the first method does not need the code to be handled from the first main method whereas the second needs to be handled.

Code: Select all

package Exceptions;
public class TestClass03 {

    public static void main( String [] args ) {
        try {
            doTest();
        } catch ( MyException e) {
            System.out.println("test");
        }
    }

    static void doTest() throws MyException {
        int[] array = new int[];
        array[10] = 1000;
        doAnotherTest();
    }
    static void doAnotherTest() throws MyException {
        throw new MyException("ttetetetet");
    }
}

class MyException extends Exception {
    public MyException( String args ){
        super( args );
    }
}
package Exceptions;

class MyException extends Exception {}

public class TestClass02 {

public static void main(String [] args ) throws MyException {
TestClass02 tc = new TestClass02();

try {
tc.m1();
} catch ( MyException myec ) {
tc.m1();;
} finally {
tc.m2();
}
}

public void m1() throws MyException {
throw new MyException();
}

public void m2() throws MyException {
throw new MyException();
}
}

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

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

Post by admin »

Not really clear about what you are asking.
If you like our products and services, please help us by posting your review here.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post by JaredTse »

Ok, maybe I was not clear, since both have a try block in the main method, which calls a one static method and the other instance method. And the instance method does handle the Exception in its method signature and the static method call does not handle it. And I want to know the reason.

public static void main(String [] args ) throws MyException

public static void main( String [] args ) {

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

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

Post by admin »

In second case, you have calls to tc.m1(); , tc.m2(); in catch block and finally. These two method calls may throw an exception and in case they do throw an exception, where will that exception go? It is not being caught anywhere in the main method, so the main method must declare it in the throws clause.

You can change the code to:

Code: Select all

try {
     tc.m1();
} catch ( MyException myec ) {

   try {
     tc.m1();
   }catch(Exception e){ }
} finally {
  try{
    tc.m2();
     }catch(Exception e){ }
}
Now, you won't need a throws clause for the main method in the second case either.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post by JaredTse »

Hi Paul,

Thank you for clarifying the flow of execution of try catch block with and how the exception class are used. I have now a clearer vision on how the checked und unchecked exceptions are handled in Java. Essentially the checked exception, that I was looking into was a method that says it will raise an exception needs to be handled by the caller. And the caller has two way of handling it. 1) by declaring it in after the method arguments 2) or in the try/catch block, but if you choose to use the try block it has to be the same. This is where I tripped I was passing different Exception other than the declared on the origin al method. Now I am going in the right direction. Thanks to you.

JT.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

Hi admin,
in one of the practice tests, i read like if you throw exception from catch block and finally block , the one thrown from catch block will be ignored and only exception from finally block will be thrown out of the method. can u please clarify on this.

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

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

Post by admin »

Please post the question id where you read it because unless I know what is the exact statement written there, it is not possible to comment on it.

Regarding the statement that you have written, what happened when you tried out a simple test program? You can throw an exception in catch as well as finally and see the output. Doing this is very important for the exam and we try to encourage readers to write test programs instead of spoon feeding.
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

this is the question id -enthuware.ocajp.i.v8.2.1210 . And when i tried ,the exception thrown from finally is thrown out of method and not the catch block. and if that is true then in this question enthuware.ocajp.i.v8.2.1276 , since the NullpointerException will be thrown out ofmethod , there is no need to delcare it in throws clause.

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

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

Post by admin »

You observed correctly and is same as what the stmt says.

Btw, NPE is a RTE. So, there is never a need to declare it in any throws clause. It is not invalid to do so though.
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

so then the correct option will be E instead of D as the exception thrown from catch block is ignored and only exception thrown from finally is propagated to the main method.and since it is runt-time exception there is no need to declare it in throws clause and program compiles fine. please clarify.

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

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

Post by admin »

The compiler doesn't know that the code in finally always throws an exception. What if it did not throw an exception? In that case the exception thrown by catch will propagate out, right? That is why a throws clause is required.
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

understood. thank you for the reply.

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests