Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 11 Exceptions and Assertions

What if you try changing the throws clause? There are many ways to change the throws clause in the overriding method, including the following:

a.Not providing any throws clause.

b.Listing more general checked exceptions to throw.

c.Listing more checked exceptions in addition to the given checked exception(s) in the base method.

If you attempt any of these three cases, you’ll get a compiler error. For example, try not providing the throws clause in the readIntFromFile() method in the class that implements the IntReader interface.

public int readIntFromFile() {

Scanner consoleScanner = new Scanner(new File("integer.txt"));

        return consoleScanner.nextInt();

}

You’ll get this compiler error: “unreported exception FileNotFoundException; must be caught or declared to be thrown.”

To summarize, the base class method’s throws clause is a contract that it provides to the caller of that method: it says that the caller should handle the listed exceptions or declare those exceptions in its throws clause. When overriding the base method, the derived method should also adhere to that contract. The caller of the base method is prepared to handle only the exceptions listed in the base method, so the overriding method cannot throw more general or other than the listed checked exceptions.

However, note that this discussion that the derived class method’s throws clause should follow the contract for the base method’s throws clause is limited to checked exceptions. Unchecked exceptions can still be added or removed from the contract when compared to the base class method’s throws clause. For example, consider the following:

public int readIntFromFile() throws IOException, NoSuchElementException { Scanner consoleScanner = new Scanner(new File("integer.txt"));

        return consoleScanner.nextInt();

}

This is an acceptable throws clause since NoSuchElementException can get thrown from the readIntFromFile() method. This exception is an unchecked exception, and it gets thrown when the nextInt() method could not read an integer from the file. This is a common situation, for example, if you have an empty file named integer.txt; an attempt to read an integer from this file will result in this exception.

Points to Remember

Here are some noteworthy points about the throws statement that could help you in the OCPJP 7 exam:

If a method does not have a throws clause, it does not mean it cannot throw any exceptions; it just means it cannot throw any checked exceptions.

It is a good practice to use the @throws JavaDoc tag to document the specific situations or cases in which an exception (both checked and unchecked) might be thrown from the method.

It is a bad practice to use a throws clause to list unchecked exceptions that a method may throw. Why? Since the compiler cannot force the callers to handle unchecked exceptions, it does not make sense to list them in the throws clause. Rather, if a method can throw an unchecked exception, it is better to use the @throws clause to document that possibility.

346

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]