• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why finally block is not executed completely?

 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In above program finally block does not executes completely.
Line 8 is not executed.
But in java finally block always executes whether exception is thrown
or not.
Then why above finally blocks terminates incompletely and line 8
is never executed.

Thanks
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you got the finaly calling a method,m();, that throws another error.
so the System.out.println("finally"); //line 8 is never executed.

ps: get rid off the m(); in the finally block then it will be completed.
[ August 09, 2006: Message edited by: Firman Drage ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to handle the exception within finally block other wise it will come out of the block when it will encounter the exception.
public class j
{
public static void main(String ar[])throws Exception
{
try{
m();
}
finally
{
try{

m();

}
catch(Exception e)
{
System.out.println ("Exception is handled here");
}
System.out.println ("inside finally block");
}
}
static void m()throws Exception
{
throw new Exception();
}
}
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gowher,

Originally posted by gowher amin naik:
[CODE]
But in java finally block always executes whether exception is thrown
or not.



In your code example the Exception is occurring inside the finally block. The Exception is not caught and goes up to main and then the program exits. So the program never gets to finish the finally block. If you call m() from inside the first try block (comment the second m() ) you will see finally being printed and an exception thrown (because there is no catch clause).
I hope this helps.

Finner
 
Mamata Mohanta
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u have to handle the exception inside finally block

public class j
{
public static void main(String ar[])throws Exception
{
try{
m();
}
finally
{
try{

m();

}
catch(Exception e)
{
System.out.println ("Exception is handled here");
}
System.out.println ("inside finally bloc");
}
}
static void m()throws Exception
{
throw new Exception();
}
}
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gowher,

Your finally Block has a call to a method which inturn throws an exception. This exception is not caught . Any uncaught exception is handled by the default handler provided by the java run-time system. This prints the exception and terminates the program and the function you called in the finally block is never returned. so whatever you do after the call to the fucntion m() will not be executed.

just add a try-catch in method m as given below

static void m()throws Exception{

try {
throw new Exception();
} catch(Exception e) {
System.out.println("Caught");
}
}
 
elangomaran kathirvel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gowher,

Your finally Block has a call to a method which inturn throws an exception. This exception is not caught . Any uncaught exception is handled by the default handler provided by the java run-time system. This prints the exception and terminates the program and the function you called in the finally block is never returned. so whatever you do after the call to the fucntion m() will not be executed.

just add a try-catch in method m as given below

static void m()throws Exception{

try {
throw new Exception();
} catch(Exception e) {
System.out.println("Caught");
}
}
    Bookmark Topic Watch Topic
  • New Topic