In cityFinals you should call exceptionGeneratorrunCourse If
In cityFinals() you should call exceptionGenerator.runCourse(). If no exception is thrown, print out \"No exception thrown\". If the call to runCourse() resulted in a checked exception, print out \"Caught checked exception\". If runCourse() resulted in an unchecked exception, print out \"Caught unchecked exception\".
In mountMidoriyama() you will handle the results of the grand finale. If the throwChecked parameter is true, your code should raise a RopeClimbException. If the throwUnchecked parameter is true instead, your code should raise a TimeException. If both parameters are false, nothing else needs to be done.
Solution
solution
public class AmericanExceptionWarrior {
static boolean throwChecked;
static boolean throwUnchecked;
/**
* Method which attempts to complete the city finals by conquering the
* Qualifying Course. QualifyingCourse has a single method:
* {@code runCourse()}. When this method is called, it could throw a checked
* exception ({@code RopeClimbException}), throw an unchecked exception (
* {@code TimeException}), or not throw any exception. If an exception is
* thrown, the method should catch the exception and print
* \"Caught checked exception\" or \"Caught unchecked exception
* \" (as appropriate). If no exception is thrown, the method should print \"
* No exception thrown\"
*
* @param exceptionGenerator
* Class whose method, runCourse(), will be used to help test
* this Exception Warrior.
* @throws
*/
public void cityFinals(QualifyingCourse exceptionGenerator) {
try {
exceptionGenerator.runCourse();
} catch (Throwable e) {
if (e instanceof Exception) {
throwChecked = true;
System.out.println(\"caught checked exception\");
} else if (e instanceof Error) {
throwUnchecked = true;
System.out.println(\"caught unchecked exception\");
}
}
}
/**
* Method to conquer the course at the grand finale -- throwing exceptions
* on your own. This has two parameters: throwChecked and throwUnchecked. If
* throwChecked is true, the method should raise a
* {@code RopeClimbException}. When throwUnchecked is true, the method
* should raise a {@code TimeException}. When both parameters are false, the
* method should not raise any exceptions.
*
* @param throwChecked
* When true, the method should raise a {@RopeClimbException
* }.
* @param throwUnchecked
* When true, the method should raise a {@TimeException
* }.
*/
public void mountMidoriyama(boolean throwChecked, boolean throwUnchecked)
throws RopeClimbException {
if (throwChecked == true) {
throw new RopeClimbException();
}
if (throwUnchecked == true) {
throw new TimeException();
}
}
public static void main(String[] args) throws RopeClimbException {
QualifyingCourse course = new QualifyingCourse();
AmericanExceptionWarrior warrior = new AmericanExceptionWarrior();
warrior.cityFinals(course);
warrior.mountMidoriyama(throwChecked, throwUnchecked);
}
}
import java.util.Scanner;
public class QualifyingCourse {
public void runCourse() throws Throwable {
System.out
.println(\"enter the no for checking the exceptions -ve for checked ,+ve for unchecked and 0 for nothing\");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
if (i < 0) {
throw new RopeClimbException();
} else if (i > 0) {
throw new TimeException();
} else
System.out.println(\"no exception is thrown\");
}
}
public class RopeClimbException extends Exception {
}
public class TimeException extends Error {
}
output
enter the no for checking the exceptions
0
no exception is thrown
enter the no for checking the exceptions -ve for checked ,+ve for unchecked and 0 for nothing
-4
Exception in thread \"main\" caught checked exception
RopeClimbException
at AmericanExceptionWarrior.mountMidoriyama(AmericanExceptionWarrior.java:58)
at AmericanExceptionWarrior.main(AmericanExceptionWarrior.java:72)
enter the no for checking the exceptions -ve for checked ,+ve for unchecked and 0 for nothing
8
caught unchecked exception
Exception in thread \"main\" TimeException
at AmericanExceptionWarrior.mountMidoriyama(AmericanExceptionWarrior.java:61)
at AmericanExceptionWarrior.main(AmericanExceptionWarrior.java:72)


