Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 3 May 2017

Java Finally

 

Java Finally Block

Finally block is the mostly asked question in the interviews. 

Finally is a block in java exception handling concept. Java finally block is used to execute important java code such as stream, closing connection, closing any file etc.

Java finally block is always executed.

Java finally block is always executed whether exception is occur or not.

Java finally block is always executed whether exception is handled or not.

The main use of java finally block is clean up code.


Some Important Points About Finally Block

  1. Java finally block is always executed.
  2. Java finally never used to catch any exception in java.
  3. Now you can have a try block with only finally block without catch block.
  4. If you are having a finally block with a catch block then it must be the last block. finally block cannot be used between try and catch block.
  5. Finally block is an optional block.
  6. You can use multiple catch block with single try block but you can use only one finally block with single try.

Let's take look in below diagram how to executes finally block in different cases.

Java Finally

Syntax of finally block 1 :

Syntax of finally block only with try block.

try
{
//statements
}
finally
{
//statements
}

Syntax of finally block 2 :

Syntax of finally block with try and catch both.

try
{
//statements
}
catch
{
//statements 
}
finally
{
//statements
}


Java Finally Block Examples

Case 1

Java finally block with only try and exception is not occurred here.

class FinallyExample1
{
public static void main(String args[])
{
try
{
System.out.println("this is try block");
}
finally
{
System.out.println("finally block always executed");
}
}
}

output : this is try block
              finally block always executed

Case 2

Java finally with try and catch block but here exception is occurred but not handled.

class FinallyExample2
{
public static void main(String args[])
{
try

{
int a = 25/0;
System.out.println(a);
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
}
finally
{
System.out.println("finally always executed");
}
}
}

output : finally always executed
              Exception in thread "main" java.lang.......

Case 3

Finally block with try and catch where exception is occurred in try block and caught in catch block.

class FinallyExample3
{
public static void main(String args[])
{
try

{
System.out.println("try block");
int a = 25/0;
System.out.println(a);
}
catch(ArithmeticException e)
{
System.out.println("catch block");
System.out.println(e);
}
finally
{
System.out.println("finally always executed");
}
}
}

output : try block
              catch block
              java.lang.AithmeticException: / by zero
              finally always executed
         


Return Statement and Finally Block Example

class FinallyWithReturnStatement
{
public static int show()
{
try
{
return 100;
}
finally
{
System.out.println("this is finally block");
}
}
public static void main(String args[])
{
System.out.println(FinallyWithReturnStatement.show());
}
}

output : this is finally block
              100


When Finally Block Does not execute

Finally block code will not execute 
  • if the thread is dead.
  • if you calling System.exit() method in program.
  • if exception is occurred in finally block.
For example :

Let's take 3 point , If exception occurred in finally block.

class FinallyDoesNotExecute
{
public static void main(String args[])
{
try
{
System.out.println("try block");
}
finally
{
int i = 32/0;
System.out.println("finally block will not execute");
}
}
}



Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate