/* MyException.java        Authors: Koffman & Wolz
 * Demonstrates try/catch.
 * Uses Swing.
 */
import javax.swing.JOptionPane;

public class MyException {

  public static void main(String[] args) {
    String numStr =
         JOptionPane.showInputDialog("Number of children:");

    int children = 1;    // problem input, default is 1
    try {
      children = Integer.parseInt(numStr);
      int oneShare = 100 / children;
      System.out.println("Each child's share is " +
                         oneShare + " percent");
    }
    catch (NumberFormatException ex) {
      // Handle number format exception.
      JOptionPane.showMessageDialog(null,
                "Invalid integer " + numStr + " - default is 1",
                "Error", JOptionPane.ERROR_MESSAGE);
      ex.printStackTrace();
    }
    catch (Exception ex) {
      // Handle all other exception types.
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }
    finally {
      System.out.println("Number of children is " + children);
    }
  }
}

