Use "throws" keyword on callback

71 views Asked by At

In my method below, I use a method in the net.sf.extJWNL package in the callback of the .anyMatch() method of Stream<Object>. However, the method I am using throws a JWNLException. Instead of the current try-catch block, I would like to use the throws keyword for the JWNLException.

Dictionary d = Dictionary.getDefaultResourceInstance();
List<POS> POSList =
     new ArrayList<POS>(EnumSet.allOf(POS.class));
boolean isWord = POSList.stream().anyMatch(c -> {
    try {
        return d.getIndexWord(c, word) != null;
    } catch (JWNLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return true;
    }
});

Also, I can't move this to a separate method because I need local variables from this method - d and word (parameter)

1

There are 1 answers

0
MRTJ On BEST ANSWER

You may need to create a wrapper around the lambda expression, which will handle the exceptions which is being thrown from the piece of code.

Please refer to the link below

https://www.baeldung.com/java-lambda-exceptions