I am studying the OCJP exam by reading a book written by SG Ganesh and Tushar Sharma. There is text on page 346 that reads:
What if you try to change the shot proposal? There are many ways to modify the throws clause in an override method, including the following:
a. Not giving a shot proposal.
b. A list of more general checked exceptions for throw.
from. A list of checked exceptions in addition to the checked exceptions (s) in the base method.
If you try to perform any of these three cases, you will get a compiler error. For example, try not to provide the throws clause in the readIntFromFile () method in a class that implements the IntReader interface.
public int readIntFromFile() {
Scanner consoleScanner = new Scanner(new File("integer.txt"));
return consoleScanner.nextInt();
}
You will get this compiler error: "Unregistered FileNotFoundException; must be caught or declared to be thrown."
To summarize, the proposal of the throws base class method is the contract that it provides to the caller of this method:
it says that the caller must handle the listed exceptions or declare those exceptions in his throwing article. When redefining the base method, the derived method must also adhere to this contract. The calling base method is ready to handle only the exceptions listed in the base method, so the override method cannot raise more general or other than the listed excluded exceptions.
However, note that this is a discussion that suggesting derived classes of methods should follow the contract for the base method, the throws clause is limited to checked exceptions. Excluded exceptions can be added or removed from the contract compared to the offer of the base class browser. 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 throw suggestion, because a NoSuchElementException can be thrown from the readIntFromFile () method. This exception is an unchecked exception and throws it when the nextInt () method cannot read the integer from the file. This is a common situation, for example, if you have an empty file named integer.txt; attempting to read an integer from this file will result in this exception.
"". , throws, . OCA, , , throws, Exception no throws , , Checked Exceptions.
, " FileNotFoundException:
, ". , , , .