jrob8577
9/17/2012 - 4:41 AM

Lecture 2 - Example Exception 2

Lecture 2 - Example Exception 2

public class ExceptionExample2 { 
  public static void main(String[] args) { 
    int x = 10, y = 2; 
    int counter = 0; 
    boolean flag = true; 
    while( flag ) { 
      start:   // label 
      try { 
        if( y > 1 )  
          break start; 
        if( y < 0 )  
          return; 
        x = x / y; 
        System.out.println( "x : " + x + " y : "+y ); 
      } 
      catch( Exception e ) { 
        System.out.println( e.getMessage() ); 
      } 
      finally { 
        ++counter; 
        System.out.println( "finally:  Counter : " + counter ); 
      } 
      --y;  // after exception, continue here...
    }  
  } 
} 

/* 
Output:   
finally : Counter : 1 
x : 10 y : 1 
finally : Counter : 2 
/ by zero 
finally : Counter : 3 
finally : Counter : 4 
*/