2

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?

8
  • You need to catch it the body of the lambda function. Commented Feb 28, 2018 at 20:21
  • Please show me how to do that... Commented Feb 28, 2018 at 20:22
  • Your logic is flawed. getMethod() will never return null. Commented Feb 28, 2018 at 20:24
  • Note that getMethod never returns null, that all JComponents have a setFont() method since it's declared in JComponent, that NullPointerException should never, ever be caught, and the catch blocks should never, ever be empty: that just hides bugs and makes them very hard to diagnose. This code doesn't make sense. Commented Feb 28, 2018 at 20:31
  • 1
    @JBNizet it took me a while to realize that this code is actually invoking the setFont method without Reflection in .forEach(m -> m.setFont(someFont)) which makes the entire filter step even more nonsensical, as the compiler already states that the setFont method is always present for all stream elements… Commented Mar 1, 2018 at 10:54

1 Answer 1

2

It is not an exception but a compilation error.
You have to catch the lambda body that may throw the exception, not the whole stream.

Here is an example to return false in filter() for the element of the stream that has thrown the exception :

myList.stream() .filter(m -> { try { return m.getClass() .getMethod("setFont", new Class[] { Font.class }) != null; } catch (NoSuchMethodException | SecurityException e) { // log the exception return false; } }) 

You may of course use a different strategy as throwing a RuntimeException and stopping the processing.

Sign up to request clarification or add additional context in comments.

1 Comment

Mind that it’s not necessary to wrap Font.class with new Class[] { Font.class } as getMethod is a varargs method.