javacodex.com
Java Examples
Java Examples
memu home questions

Basic Try Catch Finally

This is a basic exception handling program that uses try, catch & finally blocks.

Source: (TryCatchFinally.java)

public class TryCatchFinally {
   public static void main(String[] args) {
 
      for (int count = 1; count != -1 ; count--) { 
 
         try {
           System.out.println("Inside try - count: "  + count);
           int x = 1 / count;
         } catch (Exception e){
           System.out.println("Inside catch - count: "  + count );
         } finally {
           System.out.println("Inside finally - count: "  + count );
         }
 
      }
 
   }
}
 

Output:

# java TryCatchFinally 
Inside try - count: 1
Inside finally - count: 1
Inside try - count: 0
Inside catch - count: 0
Inside finally - count: 0

Contact: javacodex@yahoo.com