aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finally() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finally() method" Watch "Finally() method" New topic
Author

Finally() method

Gagan Ahuja
Greenhorn

Joined: Jul 18, 2013
Posts: 7
hiii

can someone tell me what is the use of finally method in java??
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Gagan Ahuja wrote:hiii

can someone tell me what is the use of finally method in java??


There is no such a method.finally is a block and finalize() is a method.


My one regret in life is that I am not someone else.
- Woody Allen
Gagan Ahuja
Greenhorn

Joined: Jul 18, 2013
Posts: 7
then can yo please elobrate finally block??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Gagan Ahuja wrote:then can yo please elobrate finally block??


http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rahul Parakkat
Greenhorn

Joined: Feb 13, 2012
Posts: 8

finalize() is a method from java Object class, when there are no active references to an object "finalize" method will be called by Garbage Collector,
finalize method can be called once on any given object ( by JVM )

finally block

this block can be used to write code, that you want to execute in any case ( meaning irrespective of whether the try block throws some exception or not - the statements in finally block will be always executed unless and until there is a preceding System.exit() call in code)

in the coding scenarios that i have seen, this block is used release all the open connections and resources

just a prototype :

finally
{

if(Connection != null)
connection = null

if(Resultset != null )
Resultset = null
}

Regards
Rahul Parakkat
Rahul Parakkat
Greenhorn

Joined: Feb 13, 2012
Posts: 8

Rahul Parakkat wrote:finalize() is a method from java Object class, when there are no active references to an object "finalize" method will be called by Garbage Collector,
finalize method can be called once on any given object ( by JVM )

finally block

this block can be used to write code, that you want to execute in any case ( meaning irrespective of whether the try block throws some exception or not - the statements in finally block will be always executed unless and until there is a preceding System.exit() call in code)

in the coding scenarios that i have seen, this block is used release all the open connections and resources

just a prototype :

finally
{

if(Connection != null)
connection = null

if(Resultset != null )
Resultset = null
}

Regards
Rahul Parakkat


read the prototype as below : sorry for initial wrong way of closing connection & resultset

just a prototype :

finally
{

if(Connection != null)
connection.close()

if(Resultset != null )
Resultset.close()
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finally() method