The previous section showed how to write an exception handler for thewriteListmethod in theListOfNumbersclass. Sometimes, it's appropriate for code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception. For example, if you were providing theListOfNumbersclass as part of a package of classes, you probably couldn't anticipate the needs of all the users of your package. In this case, it's better to not catch the exception and to allow a method further up the call stack to handle it.If the
writeListmethod doesn't catch the checked exceptions that can occur within it, thewriteListmethod must specify that it can throw these exceptions. Let's modify the originalwriteListmethod to specify the exceptions it can throw instead of catching them. To remind you, here's the original version of thewriteListmethod that won't compile.To specify that// Note: This method won't compile by design! public void writeList() { PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) { out.println("Value at: " + i + " = " + vector.elementAt(i)); } out.close(); }writeListcan throw two exceptions, add athrowsclause to the method declaration for thewriteListmethod. Thethrowsclause comprises thethrowskeyword followed by a comma-separated list of all the exceptions thrown by that method. The clause goes after the method name and argument list and before the brace that defines the scope of the method; here's an example.Remember thatpublic void writeList() throws IOException, ArrayIndexOutOfBoundsException {ArrayIndexOutOfBoundsExceptionis an unchecked exception; including it in thethrowsclause is not mandatory. You could just write the following.public void writeList() throws IOException {