Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1172

Posted: Thu Jun 26, 2014 4:36 pm
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?

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

Posted: Thu Jun 26, 2014 5:55 pm
by admin
Not sure what you mean. Can you elaborate more on what exactly do you think throw statement does?

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

Posted: Fri Jun 27, 2014 11:40 am
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?

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

Posted: Fri Jun 27, 2014 1:33 pm
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"??

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

Posted: Fri Jun 27, 2014 4:06 pm
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)

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

Posted: Fri Jun 27, 2014 8:49 pm
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.

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

Posted: Sat Jun 28, 2014 3:25 am
by sarakh
Paul you are the best!!!
THANKS!