A list of all unregistered exceptions (including exceptions that are superclasses of declared exceptions)

Consider the following scenario

public class A extends Throwable {}

public class B extends A {}

public class C
{
    public void f() throws A, B
    {
        // Some condition
            throw new A();

        // Some condition 
            throw new B();
    }

    void g() 
    {
        f();
    }
}

With the code above, the compiler will warn you that it will not catch (or not declare as throws)

C.java:17: unreported exception A; must be caught or declared to be thrown
        f();
         ^

If I fix this by changing g to

void g() throws A

then I don't get any more warnings (B gets implicitly caught by catching A)

Is there a way to make a compiler report for all excepted exceptions, including base classes (like here).

One way is to change g()and declare it as

public void f() throws B, A

In this case, the compiler will report B first, and as soon as I add B to the specification throw g, it will report A.

But it’s painful because

  • This is a two-step process.
  • , f, throw.

?

, - - , , - " ?". , .

f A B . g, , g, , , g throws A throws B, A. .

, , - , . , , , , . , ?

+4
2

, . Eclipse, , . , , catch throws, Eclipse ( : ), . . , Eclipse: ^).

+1

- . , , , . , , .

, , , , is-a. :

public class Vehicle {}

public class Car extends Vehicle {}

public class Boat extends Vehicle {}

: Car - Vehicle. Boat Vehicle.

throws , catch (), , / . , , Vehicle Exception, :

try
{
    // ...
}
catch (Car c)
{
    // You caught a car. Not any vehicle, a real, honking car.
}

:

try
{
    // ...
}
catch (Vehicle v)
{
    // You caught some vehicle. Any vehicle.
}

, , , , - , , . - . - .

, , , - .. "" "" , . throws , catch . , , .

, , . -, , Checkstyle. , , , , Checkstyle , . , .

, , , , Checkstyle . RedundantThrows, , . , throws.

+3

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


All Articles