7.3. Creating a method that throws an exception

[8.4] Create and invoke a method that throws an exception

In this section, you’ll explore the need to create methods that throw exceptions. You’ll also work with the throw and throws keywords to define methods that throw exceptions.

Why do you need methods that throw exceptions? Imagine that you’re assigned the task of finding a specific book and then reading and explaining its contents to a class of students. The required sequence looks like the following:

  1. Get the specified book.
  2. Read aloud its contents.
  3. Explain the contents to a class of students.

But what happens if you can’t find the specified book? You can’t proceed with the rest of the actions without it, so you need to report back to the person who assigned the task to you. This unexpected event (the missing book) prevents you from completing your task. By reporting it, you want the originator of this request to take corrective or alternate steps.

Let’s code the preceding task as the teachClass method, as shown in figure 7.10, which uses the throw statement and throws clause. This example code is for demonstration purposes only, because it uses the BookNotFoundException exception and the locateBook(), readBook(), and explainContents() methods, which aren’t defined.

Figure 7.10. Using throw and throws to create methods that can throw exceptions

The code in figure 7.10 is simple to follow. On execution of the code throw new BookNotFoundException(), the execution of teachClass() halts. The JVM creates an instance of BookNotFoundException and sends it off to the caller of teachClass() so alternate arrangements can be made.

The throw statement is used to throw an instance of BookNotFoundException. The throws statement is used in the declaration of the teachClass() method to signal that it can throw BookNotFoundException.

Why does a method choose to throw an exception as opposed to handling it itself? It’s a contract between the calling method and the called method. Referring to the teachClass() method shown in figure 7.9, the caller of teachClass would like to be informed if teachClass() is unable to find the specified book. The teachClass() method doesn’t handle BookNotFoundException because its responsibilities don’t include working around a missing book.

The preceding example helps identify a situation when you’d want a method to throw an exception, rather than handling it itself. It shows you how to use and compare the statements throw and throws—to throw exceptions and to signal that a method might throw an exception. The example also shows that a calling method can define alternate code, when the called method doesn’t complete successfully and throws an exception. Apart from testing this logic, the exam will test you on how to create and use methods that throw checked or unchecked exceptions and errors, along with several other rules.

7.3.1. Create a method that throws a checked exception

Let’s create a simple method that doesn’t handle the checked exception thrown by it, by using the statements throw and throws. The class DemoThrowsException defines the readFile() method, which includes a throws clause in its method declaration. The actual throwing of an exception is accomplished by the throw statement:

A method can have multiple comma-separated class names of exceptions in its throws clause. Including runtime exceptions or errors in the method declaration isn’t required. Including them in the documentation is the preferred way to mention them. A method can still throw runtime exceptions or errors, without including them in its throws clause.

Exam Tip

Syntactically, you don’t always need a combination of throw and throws statements to create a method that throws an exception (checked or unchecked). You can replace the throw statement with a method that throws an exception.

7.3.2. Handle-or-declare rule

To use a method that throws a checked exception, you must do one of the following:

  • Handle the exception— Enclose the code within a try block and catch the thrown exception.
  • Declare it to be thrown— Declare the exception to be thrown by using the throws clause.
  • Handle and declare— Implement both of the preceding options together.
Exam Tip

The rule of either handling or declaring an exception is also referred to as the handle-or-declare rule. To use a method that throws a checked exception, you must either handle the exception or declare it to be thrown. But this rule applies only to checked exceptions and not to unchecked exceptions.

7.3.3. Creating a method that throws runtime exceptions or errors

When creating a method that throws a runtime exception or error, including the exception or error name in the throws clause isn’t required. A method that throws a runtime exception or error isn’t subject to the handle-or-declare rule.

Let’s see this concept in action by modifying the preceding example so the readFile() method throws NullPointerException (a runtime exception) when a null value is passed to it (code changes are shown in bold in this example and throughout the rest of the chapter):

The exam might trick you by including the names of runtime exceptions and errors in one method’s declaration and leaving them out in another. (You can include the name of unchecked exceptions in the throws clause, but you don’t have to.) Assuming that the rest of the code remains the same, the following method declaration is correct:

Exam Tip

Adding runtime exceptions or errors to a method’s declaration isn’t required. A method can throw a runtime exception or error irrespective of whether its name is included in its throws clause.

7.3.4. A method can declare to throw all types of exceptions, even if it doesn’t

In the following example, the class ThrowExceptions defines multiple methods, which declare to throw different exception types. The class ThrowExceptions compiles successfully, even though its methods don’t include the code that might throw these exceptions:

class ThrowExceptions {
    void method1() throws Error {}
    void method2() throws Exception {}
    void method3() throws Throwable {}
    void method4() throws RuntimeException {}
    void method5() throws FileNotFoundException {}
}

Although a try block can define a handler for unchecked exceptions not thrown by it, it can’t do so for checked exceptions (other than Exception):

In the preceding code, method6(), method7(), method8(), and method9() compile even though their try block doesn’t define code to throw the exception being handled by its catch block. But method10() won’t compile.

Exam Tip

A method can declare to throw any type of exception, checked or unchecked, even if it doesn’t do so. But a try block can’t define a catch block for a checked exception (other than Exception) if the try block doesn’t throw that checked exception or use a method that declares to throw that checked exception.

In the next section, we’ll detail what happens when an exception is thrown and how to handle that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128