5. Exceptions
Activity 1: Designing an Exception Class Logging Data
Solution
- Import the relevant classes needed for this program to work:
NoSuchFileException
andlogging
.import java.nio.file.NoSuchFileException; import java.util.logging.*;
- Make your own methods for issuing exceptions, start by creating one for the
NullPointerException
case.public static void issuePointerException() throws NullPointerException { throw new NullPointerException("Exception: null pointer"); }
- You will also need a method for the case the file is not found:
NoSuchFileException
.public static void issueFileException() throws NoSuchFileException { throw new NoSuchFileException("Exception: file not found"); }
- Back to the
Main
method, create thelogger
object that will be reporting the exceptions and their severity level.Logger logger = Logger.getAnonymousLogger();
- Capture the argument passed to the script via the CLI...