Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

confused about the output of the program on exceptions?  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test {
public static void main(String args[]) {
int i=1, j=1;
try {
i++;
j--;
if(i/j > 1)
i++;
}
catch(ArithmeticException e) {
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(1);
}
catch(Exception e) {
System.out.println(2);
}
finally {
System.out.println(3);
}
System.out.println(4);
}
}

The above program produces the output as shown below:

0
3
4

I was expecting the output as:
0
3

After the execution of the "finally" block the program should have terminated but its not doing so....WHY..?Please help.......
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.
 
Bartender
Posts: 4089
54
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Naveen Megharaj wrote:
After the execution of the "finally" block the program should have terminated but its not doing so....WHY..?Please help.......



When the program reaches the point where it throws an exception execution jumps out of the try block which the exception occurs and either you catch it (as happens in this case) or throw it to the caller. Here as you are catching it inside the method(main in this case) so, the execution continues from end of the finally block.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your program throws any exceptions then it will handle by try catch block and it wont be affect the entire program.
It will be continue the process until it reaches the end.

finally block does not consider about the exception . It will be excuting even there is no exception in program
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!