• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

doubts???????

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx anu and pratiba for clearing my doubts.
 
Shiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic