Categories: Keyword in Java

The finally keyword in Java programming language

The finally keyword in Java programming language

We will learn in this tutorial  about The finally keyword in Java programming language

The finally is a keyword of Java, is an optional part of a try block using exception handling in Java. When an exception is thrown inside a try block, the exception will be compared to any of the catch parts of the inside in try block .when the compare exception with catch block, the exception may match or not any catch block.

However, Java finally block is having a block of code.which is always executed whether the exception is handled or not

The finally clause is optional part in the try block. However, each try block requires at least one catch or finally clause.

try{
block of code......
catch{
exception 1.....
}
catch{
exception 2.....
}
................
.................
catch{
exception n.....
}
finally
{
System.out.print(" I am a finally block");
System.out.print(" I always executed exception handled or not");
}
}

Program

public class try_keyword{
    public static void main(String args[]){
        try{//try block for exception handling
            int a[]=new int[5];
            a[6]=45;

            }
            finally{System.out.println("I always executed\n");
    }
    }
}

 

When the above code is compiled and executed, it will produce the following result

 

Output

 

In the program, the finally block contains an important statement that must be executed whether an exception occurs or not

finally block always to be associated with the try block, never we can use finally without the try block

 

There are other Java language keywords that are similar to this keyword

try keyword in Java

Catch keyword in Java

implements keyword in java

public Key words in Java

static keyword in Java language

Volatile Keyword in Java

Transient keyword in Java

Package keyword in Java

class keyword in Java

boolean keyword in Java

break keyword in Java

do keyword in Java

double keyword in Java

This keyword in Java

final keyword in Java

switch keyword in Java

interface keyword in java

extends keyword in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Usage of final keyword in Java

Usage of this keyword in Java

 

 

Suggested for you

Keywords in Java language

Keywords in C++ language

Keywords in C language

Keywords in Python language

 

The try keyword in Java programming language
The extends keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago