This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts???????

 
Neha Sawant
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
please would anyone clear my doubt.
1)
assume x=0
public class testclass
{
public void test(int x)
{
loop:for(int i=1;i<5;i++)
{
for(int j=1;j<5;j++)
{
System.out.println(i);
if(x==0)
{continue loop;
}
System.out.println(j);//line 1
}
the output given is 1234.
but i think it should be compile time error as the statement
on line 1 is never reached.
2)
when an exception is caught in the catch clause and after the finally is executed do the remaining statements execute.
example
public testclass
{
public static void main(String args[])
{
int k=0;
try
{
int j=5/k;
}
catch(ArithmeticException e)
{System.out.println("1");
}
catch (RuntimeException e)
{System.out.println("2");
]
finally
{
System.out.println("3");
}
System.out.println("4");
}
}
Will the output be 134 or 13
 
Anu Kris
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.for the first doubt,i think the coding is incomplete and the answer 1234 gets printed which is the value of i.
2.after the exception is caught ,finally will be executed,even if the exception was not caught finally will be executed and the normal flow of the program will continue.So the result is 134
 
Pratibha Enjeti
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! Neha,
Your first question, your sample code does look incomplete,
try the program below which does the same thing, which gives you the output : 1 2 3 4, not a compile time error.
Reason: x is always =0, when i=1 , the condition matches and "continue loop" is executed, continue statement always breaks out of the present iteration and control reaches back to the label statement pointed by Continue. Hence,increments to the next iteration.
Here, "Continue loop" breaks out of the inner loop j , never reaching the print statement and increments the value of i=2.
Prints the value and "continues" the "loop" or the label statement.
When i =4 it follows the same procedure and i is incremented to 5. Now the outer loop has reached the limit to the program exits out, never reching Print j statement.
-Its ok for code to not do anything sometimes as long as the syntax is ok. Generally you would not code that way.
Here's the code:
public class testclass
{
static int x =0;

public static void main(String args[])
{
int j;
loop:for(int i=1;i<5;i++){

for(j=1;j<5;j++) {
System.out.println(i);
if(x==0){
continue loop;
}
System.out.println(j);//line 1
}
}
}
}

}

2. After finally statement in your eg. o/p will be 134. Program flow does continue after the finally statement. JLS has some good titbits on Exception handling , which you can refer to !
I hope my explanations help you.
Pratibha.
 
Neha Sawant
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx anu and pratiba for clearing my doubts.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic