/* Author: James Tam Date: October 16, 2003 A program to illustrate how the try-catch-finally block works. */ import java.io.*; public class TCFExample { /* When this method is run two main possibilities may occur: 1) All the statements in the try block sucessfully execute from start to finish. 2) Part way through the execution of the statements in the try block an exception is thrown. In both cases the statements in the finally clause will be executed even though the try block and the catch block both have return statements. */ public void method () { BufferedReader br; String s; int num; // Within the try block we "try" to perform file input/output operations. Should an // error ever occur during the file operations, the program will break out of the // the try block and the code within the catch block executes. try { System.out.print("Type in an integer: "); br = new BufferedReader(new InputStreamReader(System.in)); s = br.readLine(); num = Integer.parseInt(s); return(); } catch (IOException e) { e.printStackTrace(); return(); } catch (NumberFormatException e) { e.printStackTrace (); return(); } // This block of code is always executed regardless of whether an exception occured or not. finally { System.out.println("<<>>"); return(); } } }