Question: Please use comments to explain your program. Description of program: Propagating and Catching an Exception: In this exercise you're going to create two methods that
Please use comments to explain your program.
Description of program:
Propagating and Catching an Exception:
In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main() method, which will call another method. If an exception is thrown in the other method, main() must deal with it. A finally statement will be included to indicate that the program has completed. The method that main() will call will be named reverse, and it will reverse the order of the characters in a String. If the String contains no characters, reverse will propagate an exception up to the main() method.
Create a class called Propagate and a main() method.
Create a method called reverse. It takes an argument of a String and returns a String. In reverse, check if the String has a length of 0 by using the String.length() method. If the length is 0, the reverse method will throw an exception. Now include the code to reverse the order of the String.
Now in the main() method you will attempt to call this method and deal with any potential exceptions. Additionally, you will include a finally statement that displays when main() has finished.
B. Creating an Exception:
In this exercise we attempt to create a custom exception. We won't put in any new methods (it will have only those inherited from Exception), and because it extends Exception, the compiler considers it a checked exception. The goal of the program is to determine whether a command-line argument, representing a particular food (as a string), is considered bad or OK.
Let's first create our exception. We will call it BadFoodException. This exception will be thrown when a bad food is encountered.
Create an enclosing class called MyException and a main() method.
Create a method called checkFood(). It takes a String argument and throws our exception if it doesn't like the food it was given. Otherwise, it tells us it likes the food. You can add any foods you aren't particularly fond of to the list.
Now in the main() method, you'll get the command-line argument out of the String array, and then pass that String on to the checkFood() method. Because it's a checked exception, the checkFood() method must declare it, and the main() method must handle it (using a try/catch).
Do not have main() declare the exception, because if main() ducks the exception, who else is back there to catch it?
Upload the .java files into D2L.
Following links will help you to understand how to deal with command-line arguments:
https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
http://www.javawithus.com/tutorial/taking-input-as-command-line-arguments
http://www.cs.colostate.edu/helpdocs/eclipseCommLineArgs.html
http://introcs.cs.princeton.edu/java/15inout/windows-cmd.html
https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
http://www.cs.princeton.edu/courses/archive/spr04/cos126/hello/mac.html
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
