Kotlinlearncs.online LogoJava
Return to List

Solve: Exception Recorder

Created By: Pranav Narayanan
/ Version: 2025.2.0

Create a public class ExceptionRecorder that provides a single class method named recordExceptions. recordExceptions accepts an instance of the Faulter class which provides a fault method. fault takes no arguments, but the first N times that it is called on a Faulter instance, it will throw some kind of exception. After that point, it will return an int.

Your solution should repeatedly call fault until it returns a value, and then you should return that same value. However, every time fault throws an exception, on a new line you should print the attempt number and exception type. If, for example, you are on attempt 5 and fault throws an IllegalStateException, you should print "Attempt 5: Threw IllegalStateException". Your first attempt should be attempt 1, NOT attempt 0.

Note that given some exception e, calling e.getClass().getName() will return the exception type. For example, if e was an IllegalStateException, e.getClass().getName() returns "java.lang.IllegalStateException". You will need to modify this slightly for your output.

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/java/exception-recorder/[email protected]