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

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

Moderator: admin

Post Reply
coelhorrc
Posts: 1
Joined: Tue Dec 03, 2013 5:38 pm
Contact:

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

Post by coelhorrc »

Why does the following code prints 5? I thought that every exception would stop the thread.

int k = 0;
try{
int i = 5/k;
}
catch (ArithmeticException e){
System.out.println("1");
}
catch (RuntimeException e){
System.out.println("2");
return ;
}
catch (Exception e){
System.out.println("3");
}
finally{
System.out.println("4");
}
System.out.println("5");

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

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

Post by admin »

If an exception is left uncaught, then that stops the thread. But here, we are catching the exception. You might want to check this out: http://www.javamex.com/tutorials/except ... dler.shtml

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

Deleted User 1713

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

Post by Deleted User 1713 »

In the explanation to the question it sais: "Remember : finally is always executed even if try or catch return; (Except when there is System.exit() in try.)"

You can also have System.exit() in the catch clause. This will prevent finally from being executed as well.

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

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

Post by admin »

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

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

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

Post by Kevin30 »

If I put a return statement in the catch (AritmeticException e) clause instead of the catch (RuntimeException e) clause, then 5 will not print:

Code: Select all

public class TestClass{
   public static void main(String args[]){
      int k = 0;
      try{
         int i = 5/k;
      }
      catch (ArithmeticException e){
         System.out.println("1");
         return ;
      }
      catch (RuntimeException e){
         System.out.println("2");
      }
      catch (Exception e){
         System.out.println("3");
      }
      finally{
         System.out.println("4");
      }
      System.out.println("5");
   }
}
The printout here is:
1
4

instead of:
1
4
5

Can you tell me what the logic is with regard to the return statement?
I'm guessing that the following things will happen, in order:
- of course the ArithmeticException is being caught,
- then 1 gets printed,
- then it encounters the return statement,
- but finally always runs (except in case of an System.exit()) so 4 gets printed,
- and now what? the code returns to where?

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

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

Post by admin »

The control returns to whoever called the method. In this case, it is the JVM that invoked the main method. So the control goes back to the JVM.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests