Copy, run, and study the TestExceptions Java application Which is below. Modify
ID: 3760997 • Letter: C
Question
Copy, run, and study the TestExceptions Java application Which is below. Modify the program so that it gets its inputs from the user via the standard input stream rather than the command-line. In addition, a new user-defined Exception class is added to the program.
Write a private static method named menu() that returns a String object. The method must declare that it throws the GoofyUserException.
The menu() method prints the following to the standard output stream.
Throw a GoofyUserException object if the user enters an invalid choice. The main() method needs to be modified to catch the GoofyUserException. Print the following in the catch block for GoofyUserException.
Return following String objects depending on user input.
Exit the program using System.exit(0) if menu() returns null (i.e. the user enters an 8).
If choice 2 entered, prompt the user to enter either a number or a word. Append this input to the return String. Note: There must be a space between the "-d" and the input that is appended.
The modified program should not have any code related to the NeedsHelpException.
Testing the Program
The modified program can be tested by comparing its output with the output produced when the TestExceptions program is executed.
Explanation / Answer
import java.io.*; import java.lang.Math; public class TestExceptions { public static void main(String[] argv) { String m = " -d 0 force FloatingPointDivideByZeroException" + " -d n execute a floating-point divide by 0" + " -p force a NullPointerException" + " -r force a DummyRuntimeException" + " -t r force a runtime exception to be thrown up" + " -t e force exception to be thrown up" + " -t E force exception from finally block"; try { if (argv.length == 0) throw new NeedsHelpException(m); if (argv[0].equals("-d")) divide(Integer.parseInt(argv[1])); else if (argv[0].equals("-p")) nullPointer(); else if (argv[0].equals("-r")) dummyRuntime(260); else if (argv[0].equals("-t")) { ThrowUp t = new ThrowUp(); t.throwUp(argv[1].charAt(0)); //new ThrowUp().throwUp(argv[1].charAt(0)); } else System.out.println("everything is fine in the try block"); } catch(FloatingPointDivideByZeroException e1) { // getMessage() and printStackTrace() are Throwable methods System.out.println("getMessage(): " + e1.getMessage()); e1.printStackTrace(System.out); } catch(NullPointerException e2) { e2.printStackTrace(); // goes to standard error } catch(DummyRuntimeException e3) { System.out.println(e3.getMessage() + "; id=" + e3.id); } catch (EOFException e4) { System.out.println("in EOFException catch block of the main()"); e4.printStackTrace(); } catch(Exception e) { System.out.print("catching everything"); if (e instanceof IllegalArgumentException) System.out.print(" (IllegalArgumentException)"); System.out.println(); e.printStackTrace(); } finally { System.out.println("in finally block"); } } private static void divide(int denom) throws FloatingPointDivideByZeroException { if (denom == 0) throw new FloatingPointDivideByZeroException("divide by 0"); // floating-point divide-by-0 allowed System.out.println("1997.10 / 0 equals " + 1997.1 / 0d); } private static void dummyRuntime(int i) /* throws DummyRuntimeException */ { throw new DummyRuntimeException("dummyRuntime", i); } private static void nullPointer() throws Exception /* not really, but maybe in future */ { String s = null; if (s.equals("csc260")) ; } } class FloatingPointDivideByZeroException extends Exception { public FloatingPointDivideByZeroException(String m) { super(m); } } class DummyRuntimeException extends RuntimeException { public int id; public DummyRuntimeException(String m, int i) { super(m); id = i; } } class NeedsHelpException extends Exception { public NeedsHelpException(String m) { super(m); } } class ThrowUp { public void throwUp(char c) throws EOFException, Exception { try { if (c == 'r') throw new IllegalArgumentException("throw up"); else throw new EOFException("throw up"); } catch(EOFException e) { System.out.println("in ThrowUp.throwUp()"); e.printStackTrace(); throw e; } finally { System.out.println("in ThrowUp.throwUp().finally"); if (c == 'E') //cause an exception to get lost throw new Exception(); } } }