EDIT . This serves as a general decision on how to declare checked exceptions. As others have pointed out, you should catch a NoSuchMethodError or even an IncompatibleClassChangeError , because that is what will be thrown at runtime.
Since you want to associate yourself with another version of this library, and not compile it (it is not recommended by itself, but I do not judge), you need to convince the compiler that everything is in order.
One way to do this in this case is to create a static helper declaring an exception:
class Util { public static void unsafeApiCall() throws NoSuchMethodException {
use as
try { Util.unsafeApiCall(); do.something(); } catch(NoSuchMethodException e2) { System.out.println("!"); }
It also stands out visually, which is good.
EDIT2 . This can be inconvenient if you select excluded exceptions without declaring them, as described here .
source share