Tuesday 7, September 2010
      Home       Tutorials       Free Ebooks       Free Scripts       Tech News       Articles       About Us

The finally clause

Once a Java virtual machine has begun to execute a block of code -- the statements between two matching curly braces -- it can exit that block in any of several ways. It could, for example, simply execute past the closing curly brace. It could encounter a break, continue, or return statement that causes it to jump out of the block from somewhere in the middle. Or, if an exception is thrown that isn't caught inside the block, it could exit the block while searching for a catch clause.
Given that a block can be exited in many ways, it is important to be able to ensure that something happens upon exiting a block, no matter how the block is exited. For example, if you open a file in a method, you may want to ensure the file gets closed no matter how the method completes. In Java, you express such a desire with a finally clause.
To use a finally clause, you simply: (1) Enclose the code that has multiple exit points in a try block; and (2) place the code that must be executed when the try block is exited in a finally clause.
Here's an example:

try {
    // Block of code with multiple exit points
}
finally {
    // Block of code that must always be executed when the try block
    // is exited, no matter how the try block is exited
}

At least one clause, either catch or finally, must be associated with each try block. If you have both catch clauses and a finally clause with the same try block, you must put the finally clause after all the catch clauses, as in:

// In Source Packet in file except/ex8/VirtualPerson.java
class VirtualPerson {
    public void drinkCoffee(CoffeeCup cup) {
        try {
            int i = (int) (Math.random() * 4.0);
            switch (i) {
            case 0:
                throw new TooHotException();
            case 1:
                throw new TooColdException();
            case 2:
                throw new UnusualTasteException();
            default:
                System.out.println("This coffee is great!");
            }
        }
        catch (TooHotException e) {
            System.out.println("This coffee is too hot.");
        }
        catch (TooColdException e) {
            System.out.println("This coffee is too cold.");
        }
        catch (UnusualTasteException e) {
            System.out.println("This coffee is too strong.");
        }
        finally {
            System.out.println("Can I please have another cup?");
        }
    }
    //...
}

If during execution of the code within a try block, an exception is thrown that is handled by a catch clause associated with the try block, the finally clause will be executed after the catch clause. For example, if a TooColdException exception is thrown during execution of the try block above, the program would print the following:

This coffee is too cold.
Can I please have another cup?

If an exception is thrown that is not handled by a catch clause associated with the try block, the finally clause is still executed. The Java virtual machine will execute the code of the finally clause before it continues searching elsewhere for an appropriate catch clause. There is no way to leave a try block without executing the code of its finally clause.
You can do anything inside a finally clause that you can do elsewhere, including executing break, continue, or return statements, or throwing exceptions. Such actions inside a finally clause, however, can have some surprising effects. For example, consider a finally clause that is entered because of an uncaught exception. If the finally clause executes a return, the method would complete normally via the return, not abruptly by throwing the exception. The exception would have in effect been handled by the finally clause instead of a catch clause.
As another example, consider a finally clause that is entered because a return true; statement was executed inside the try block. If the finally clause executes a return false; statement, the method will return false.

   
 
Java Language Tutorial
 
  What is JAVA
What is JAVA
  Java an Object Oriented Language
Java an Object Oriented Language
  Java Keywords
Java Keywords
  Data Types
Data Types
  Java Operators
Java Operators
  Control Structure
Control Structure
  Arrays
Arrays
  Java Classes
Java Classes
  Java Constructors
Java Constructors
  Java Methods
Java Methods
  Java Method Overloading
Java Method Overloading
  Java Method Overriding
Java Method Overriding
  Java Nested Classes
Java Nested Classes
  Inheritance in Java
Inheritance in Java
  Interfaces in Java
Interfaces in Java
  Polymorphism in Java
Polymorphism in Java
  Exception Handling in Java
Exception Handling in Java
  Catching Exceptions
Catching Exceptions
  Embedding information in an exception object
Embedding information in an exception object
  The throws clause
The throws clause
  Checked and unchecked exceptions
Checked and unchecked exceptions
  The finally clause
The finally clause
  Applets in Java
Applets in Java
  Running an Applet
Running an Applet
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download develop your own database software  Ebook develop your own database software
   
Download Article Marketing Profits Ebook Article Marketing Profits
   
Download Data Structures and Algorithms with Object-Oriented Design Patterns in C++  Ebook Data Structures and Algorithms with Object-Oriented Design Patterns in C++
   
Download PHP Essentials  Ebook PHP Essentials
   
Download Make Money With Resale Rights Products Ebook Make Money With Resale Rights Products
   
Technology News      
Studiesinn.com © 2010 All Rights Reserved.