Java overriding throws override clause

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: , ". , , , .

+4
5

, :

class A {
    public void foo() throws FileNotFoundException {

    }
}

class B extends A {
    public void foo() {

    }
}

. Java Language.

+2

, . , :

interface A {
    void meth() throws IOException;
}

class B implements A {
    @Override
    void meth() throws FileNotFoundException { } // compiles fine
}

class C implements A  {
    @Override
    void meth() { } // compiles fine
}

class D implements A  {
    @Override
    void meth() throws Exception { } // compile error
}
+2

A

A.

:

class A {
    void method() throws GeneralSecurityException {
    }
}

class B extends A {
    void method() {
    }
}

B A.

C

C: , , .

class C extends A {
    void method() throws NoSuchAlgorithmException, InvalidKeyException {
    }
}

NoSuchAlgorithmException InvalidKeyException GeneralSecurityException.

C C.

JLS

Java Language, Java SE 8 Edition, 11.2:

...

(§11.1.1), throws, . throws , , throws (§8.4.8.3).

...

8.4.8.3 :

...

  • m2 throws, , m1 throws .
  • , throws m2, (§4.6) throws m1; .

...

JLS

JLS . , , , , . , . , , , ; .

, RuntimeException .

, , , .

+1

. , , , :

public int readIntFromFile() {
    Scanner consoleScanner = new Scanner(new File("integer.txt"));
    return consoleScanner.nextInt();
} 

, Scanner FileNotFoundException, readIntFromFile(). , readIntFromFile() , .

This applies to subclass / base class classes only if the method in the subclass calls its super method:

class A {
    void meth() throws IOException {
        // ...
    }
}

class B extends A {
    @Override
    public
    void meth() { 
        super.meth(); // compile error here
        // ...
    }
}
0
source

java.io.Printwritter/PrintStream :: close()api in java is the best example for IOExceptionbeing thrown into a superclass ( java.io.OutputStream :: close()), is ignored here in the subclass.

And one more thing

try {
   //CheckedExceptionX must be thrown from here
}
catch(CheckedExceptionX e) {
}

but

public void someAPI throws CheckedExceptionX {
     //trowing CheckedExceptionX is not mandatory in this method
     //Means this body can be free from throwing that mentioned CheckedException 
}
0
source

Source: https://habr.com/ru/post/1606047/


All Articles