Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java finally block

  • The finally block in Java is utilized to execute essential code, such as closing connections and performing other necessary tasks.
    • Whether or not an exception is handled, the Java finally block is always executed. As a result, it contains all of the necessary statements that must be printed regardless of whether or not the exception occurs.
    • The finally block is executed after the try-catch block in Java Exception Handling.
    • If you do not handle the exception, JVM executes finally block before terminating the program.






Flow Chart of Java finally block


Java finally block

Why is the Java finally block used?

  • The use of finally blocks is essential for incorporating "cleanup" code, including actions like closing a file or terminating a connection. These blocks ensure that necessary tasks are executed regardless of whether an exception occurs or not. By employing finally blocks, Java Exception Handling becomes more efficient and reliable, enabling developers to manage resources effectively.
  • The final block can contain the important statements to be printed on console.

Java finally block


public class Main {	
    public static void main(String args[]){    	  		  
	    try{   // try block  
            int arr[]=new int[10];    
            arr[15]=15/0;    
            System.out.println(arr[15]);    
	    }  
	    catch(ArithmeticException e){   
	      System.out.println(e);  
	    }  
	    catch(ArrayIndexOutOfBoundsException e){   
	       System.out.println(e);  
	    } 
	    finally {  
	    	System.out.println("The finally block is always executed.");  
	    }  
	  System.out.println("This Code after finally block");  
   } 
}




Output:

 java.lang.ArithmeticException: / by zero
The finally block is always executed.
This Code after  finally block 


When an exception occurs but not handled by the catch block


package DockerTpoint;
public class Main {	
    public static void main(String args[]){    	  		  
	    try{   // try block  
	    	System.out.println("Inside try block");
            int Num=15/0;    
            System.out.println(Num);    
	    }  
	    catch(ArrayIndexOutOfBoundsException e){  //it cannot handle Arithmetic type exception  
	       System.out.println(e);  
	    } 
	    finally {  
	    	System.out.println("The finally block is always executed.");  
	    }  
	  System.out.println("This Code after "
	  		+ " finally block");  
   } 
}



Output:

 Inside try block
The finally block is always executed.
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at HELLO/DockerTpoint.Main.main(Main.java:7) 




When an exception does not occur finally block


The try block in the example below has two exceptions. However, only one exception occurs at a time, and its associated catch block is executed.


package DockerTpoint;
public class Main {	
    public static void main(String args[]){    	  		  
	    try{   // try block  
	    	System.out.println("Inside try block");
            int Num=15/3;    
            System.out.println(Num);    
	    }  
	    catch(ArrayIndexOutOfBoundsException e){  //it cannot handle Arithmetic type exception  
	       System.out.println(e);  
	    } 
	    finally {  
	    	System.out.println("The finally block is always executed.");  
	    }  
	  System.out.println("This Code after "
	  		+ " finally block");  
   } 
}



Output:

 Inside try block
5
The finally block is always executed.
This Code after  finally block 
Java Throws Keyword Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
custom-exception

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.