I have this code
List<JComponent> myList = new ArrayList<>(); fillmyList(myList); //Some method filling the list try{ menuList.stream() .filter(m->m.getClass().getMethod("setFont", new Class[]{Font.class}) != null) //unreported exception NoSuchMethodException; must be caught or declared to be thrown .forEach(m -> m.setFont(someFont)); } catch (NullPointerException | NoSuchMethodException e) {} //exception NoSuchMethodException is never thrown in body of corresponding try statement But, I have this error messages:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - exception java.lang.NoSuchMethodException is never thrown in body of corresponding try statement How solve this?
getMethod()will never return null.setFontmethod without Reflection in.forEach(m -> m.setFont(someFont))which makes the entirefilterstep even more nonsensical, as the compiler already states that thesetFontmethod is always present for all stream elements…