package example6_3_ExceptionTypes; import javax.swing.JOptionPane; public class ExceptionTypes { // public static void main(String[] args) { // int theInteger = 0; // String message = "Please enter an integer: "; // while(theInteger == 0) { // String line = JOptionPane.showInputDialog(message); // try { // theInteger = Integer.parseInt(line); // helperMethod(); // } catch (RuntimeException e) { // message = "Helper method acting up. Please re-enter an integer"; // } // } // // // } public static void main(String[] args) { int square; try { square = helperMethod(); } catch (NumberFormatException e) { square = 7; // "default value" } JOptionPane.showMessageDialog( null, "Thanks! Squared, that is: "+square); System.out.println("We reached the end of the program"); } public static int helperMethod() throws NumberFormatException { System.out.println("We reached the beginning of the helperMethod"); try { helpersHelper(); } catch(MyException e) { String message = "The \"helper\" " + "method is up to trouble again! He says:" + e.getMessage(); System.out.println(message); JOptionPane.showMessageDialog(null, message); e.printStackTrace(); } String message = "Please enter an integer: "; String line = JOptionPane.showInputDialog(message); int theInteger = Integer.parseInt(line); int square = theInteger * theInteger; System.out.println("We reached the end of the helperMethod"); return square; } public static void helpersHelper() throws MyException { if(Math.random() > 0.00001) { //throw new RuntimeException("Ha!"); throw new MyException("Ha!"); //"Ha!" } } }