The finally Keyword

Ads
 

The finally Keyword

The finally is a Java keyword that is used to define a block that is always executed in a try−catch−finally statement.

The finally is a Java keyword that is used to define a block that is always executed in a try−catch−finally statement.

The finally Keyword  

     

The finally is a Java keyword that is used to define a block that is always executed in a try−catch−finally statement. In java, there are three clauses named try, catch and finally used as exception handler components. A finally block typically contains cleanup code that recovers from partial execution of a try block.

It is always a good practice to use finally clause after the try and catch block to handle an unexpected exception occurred in the try block. It becomes possible because the finally block always executes when the try block exits and it is also useful to write the cleanup code within the finally block. The finally block is executed surely whether the exception is generated or not. This is a Java keyword therefore it may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program. It is used when the finally block is executed after the execution exits the try block and any associated catch clauses regardless of whether an exception was thrown or caught. 

The syntax of declaring a final type variable is: 

try {

/ Normal execution path 

  ??????.
    ??????  

} catch (ExampleException ee)

/  deal with the ExampleException

   ??????.
 
??????

}

finally {

   / This optional section is executed upon
 termination of any of the try or catch blocks /above

     ??????..
 
??????.

}
 

Ads

Ads