About Question enthuware.ocajp.i.v7.2.1172

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.1172

Post by sarakh »

When we run the program with no argument:
I understand very well that both "Exception in Main" and "The end" would get printed.
But why wouldn't we also get "Some Exception"?
Isn't that what the else throw new Exception("Some Exception"); supposed to do?

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

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

Post by admin »

Not sure what you mean. Can you elaborate more on what exactly do you think throw statement does?
If you like our products and services, please help us by posting your review here.

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

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

Post by sarakh »

So we have

Code: Select all

public class FinallyTest{
   public static void main(String args[]){
      try{
          if (args.length == 0) return;
          else throw new Exception("Some Exception");
      }
      catch(Exception e){
          System.out.println("Exception in Main");
      }
      finally{
          System.out.println("The end");
      }
   }
}
if you run the program with one arguments, then (args.length == 0) is false
so the else case is executed which is throw new Exception("Some Exception");
so the out put should be "Some Exception"
then the catch(Exception e) will catch the exception and System.out.println("Exception in Main");will be executed, which makes the "Some Exception" to be added to the out put
and then the finally, will make the "The end" ot be added to the out put.

Am I wrong?

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

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

Post by admin »

so the else case is executed which is throw new Exception("Some Exception");
so the out put should be "Some Exception"
Why do you think throw new Exception("Some Exception"); should output "Some Exception"??
If you like our products and services, please help us by posting your review here.

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

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

Post by sarakh »

well because when I compile this

Code: Select all

public class TestExp{

     public static void main(String []args) throws Exception {
        throw new Exception("Some Exception");
     }
}
I get
Compiling the source code....
$javac TestExp.java 2>&1

Executing the program....
$java -Xmx128M -Xms16M TestExp
Exception in thread "main" java.lang.Exception: Some Exception
at TestExp.main(TestExp.java:4)

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

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

Post by admin »

The output is not printed by the statement "throws new Exception()". It is printed by the JVM because it caught the exception. "throw new Exception()" doesn't print anything. It merely throws the exception. Whoever catches it is free to either print it or not. Try this code:

Code: Select all

public class TestExp{

     public static void main(String []args) {
      try{
        throw new Exception("Some Exception");
      }catch(Exception e){
         //here you have the Exception object referred to by e. You can choose to print it or do nothing with it.
         //System.out.println(e); 
         //e.printStackTrace(); //this is what the JVM does if it catches the exception that is why you see the output.
       }
     }
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

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

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

Post by sarakh »

Paul you are the best!!!
THANKS!

Post Reply

Who is online

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