Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am not trying to sound patronizing or anything. I will probably say a lot of stuff you already know. But bear with me, while I try to explain why solving your problem will not be possible using compiler warnings.</p> <p>Deriving two classes from each other, never mind if they are exception classes or not, means, they are in a <em>is-a</em> relation. For example:</p> <pre><code>public class Vehicle {} public class Car extends Vehicle {} public class Boat extends Vehicle {} </code></pre> <p>means: <code>Car</code> <em>is a</em> <code>Vehicle</code>. <code>Boat</code> <em>is a</em> <code>Vehicle</code>.</p> <p>With <code>throws</code>, or more to the point, <code>catch</code> (later on), you specify the type you want to throw/be caught. Now, assuming that <code>Vehicle</code> was derived from <code>Exception</code>, you could do this:</p> <pre><code>try { // ... } catch (Car c) { // You caught a car. Not any vehicle, a real, honking car. } </code></pre> <p>By doing this, however:</p> <pre><code>try { // ... } catch (Vehicle v) { // You caught some vehicle. Any vehicle. } </code></pre> <p>you specify that you do not care if you catch cars, boats, bicycles or whatever, as long as it is mobile and transports passengers or cargo. Because a car <em>is a</em> vehicle. And a boat <em>is a</em> vehicle.</p> <p>To sum up, <strong>what you are trying to do goes completely against the very idea of the object oriented approach.</strong> You cannot make the compiler report any "suppressed" or "hidden" exceptions, because they really are not. With <code>throws</code>, they are very much defined, and with <code>catch</code>, they are very much handled. So the compiler does not even understand your problem, let alone offer some way to prevent it.</p> <p>As the comments say, a static code analysis would pretty much be the only option to do what you want to do. Something which I have used for occasions like these, is <a href="http://checkstyle.sourceforge.net/" rel="nofollow">Checkstyle</a>. Since what you want is quite unusual, I do not think that Checkstyle has a predefined check for what you want. In fact, it DOES have a check for <a href="http://checkstyle.sourceforge.net/config_coding.html#RedundantThrows" rel="nofollow">the exact opposite</a>.</p> <p>So you will probably have to write your own check for it, but Checkstyle allows this rather easily. I would start by reading the source code of the <code>RedundantThrows</code> check, then doing the opposite of what it does. This should make your custom check to fail if you do not list ALL thrown checked exceptions in the method <code>throws</code> signature.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload