- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java finally Block
When the exceptions are thrown, execution in a method takes a rather abrupt, non-linear path that alters the normal flow by the method.
Depending upon the method is coded, it is even possible for an exception to cause the method to return ultimately. This could be a problem in several methods. For instance, if a method opens a file upon the entry and closes it upon the exit, then you will not want the code that closes the file to be bypassed by exception-handling mechanism. The keyword finally is designed to address this contingency.
The finally creates a block of code that will be executed after the try/catch block has completed and before the code following the try/catch block.
The finally block will execute always, whether an exception is thrown or not. If an exception is thrown, the finally block will execute even if no any catch statement matches the exception. Any time a method is about to return to the caller from inside the try/catch block, through an uncaught exception or an explicit return statement, the finally clause is also executed before the method returns. This can be useful in closing the file handles and freeing up any other resources that might have been allocated at the starting of a method with the intent of disposing of them before returning.
The finally clause is optional. Nevertheless, each try statement requires at least one catch or a finally clause.
Java finally Block Example
Following is an example program that shows the three methods that exit in various ways, none without executing their finally clauses :
/* Java Program Example - Java finally keyword * This program demonstrate the finally keyword */ class FinallyTest { /* Throw an exception out of method */ static void procX() { try { System.out.println("inside procX"); throw new RuntimeException("demo"); } finally { System.out.println("procX's finally"); } } /* return from within a try block */ static void procY() { try { System.out.println("inside procY"); return; } finally { System.out.println("procY's finally"); } } /* execute a try block normally */ static void procZ() { try { System.out.println("inside procZ"); } finally { System.out.println("procZ's finally"); } } public static void main(String args[]) { try { procX(); } catch(Exception e) { System.out.println("Exception caught"); } procY(); procZ(); } }
In this program, the procX() method prematurely breaks out of the try by throwing an exception. The finally clause is executed on the way out. The method procY()'s try statement is existed via a return statement. The finally clause is executed before procY() returns. In the procZ(), the try statement executed normally, without an error. The finally block is however executed.
Below is the output generated by the above Java program :
Note - If a finally block is linked with a try, the finally block will be executed upon conclusion of the try.
« Previous Tutorial Next Tutorial »