2

Eclipse compiles the following code without any problem, whereas when mvn tries to compile this code, a compilation failure results:

try {
    // Distribution.rep().get(id) returns a java.util.Optional
    Distribution updated = Distribution.rep().transact(() -> {
        Distribution distro = Distribution.rep().get(id).orElseThrow(() -> {
            throw new NotFoundException("Couldn't find Distribution with ID '%s'.", id);
        });
        // Other stuff...
    });
    rs.setData(updated);
} catch (ExecutionException e) {
    // Handle the error
} catch (Exception e) {
    // Handle the exception
}

The error thrown by maven-compiler-plugin:3.1 is:

unreported exception java.lang.Throwable; must be caught or declared to be thrown

NotFoundException extends RuntimeException. The transact method wraps all runtime (?) exceptions in an ExecutionException before throwing them. Either way, this NotFoundException (when thrown) should get caught by the catch clause for ExecutionException, so what's the problem?

From what I can understand the maven-compiler-plugin seems to think that the NotFoundException is a Throwable. NotFoundException is defined in my code (project), so maven-compiler-pluginshould know about it...

Source and target for the compiler plugin are defined as 1.8. Have Java version 1.8.0_181. Tried with maven-compiler-plugin version 3.8.0, got the same result.

markvgti
  • 4,321
  • 7
  • 40
  • 62
  • Eclipse uses its' own compiler. https://stackoverflow.com/questions/3061654/what-is-the-difference-between-javac-and-the-eclipse-compiler – Elliott Frisch Jan 11 '19 at 05:30
  • Seems to me that the code should compile with any compiler... – markvgti Jan 11 '19 at 05:33
  • `RuntimeException` makes it an *unchecked* exception. As for why eclipse allows it; it might be a corner case in the language spec (or it could be an eclipse [bug](https://i.redd.it/b44yvqgindh01.jpg)). – Elliott Frisch Jan 11 '19 at 05:39
  • Exactly, `RuntimeException` is an unchecked exception. Then why am I getting an error saying that I need to either catch it (which I am doing) or declare it in a throws clause. Neither of the questions you've pointed out solve my problem. This is NOT a duplicate. – markvgti Jan 11 '19 at 05:43
  • Or, it could be a [javac](https://stackoverflow.com/a/25533461/2970947) bug. – Elliott Frisch Jan 11 '19 at 05:46
  • Maybe it is not the `NotFoundException` but something else that is causing the error. Did you try to remove it? – Henry Jan 11 '19 at 05:51
  • 1
    I could solve the issue by supplying a type parameter to `orElseThrow()`: `Distribution.rep().get(id).orElseThrow(...` etc. If this question were not marked as duplicate I would be able to add this as the accepted answer. – markvgti Jan 11 '19 at 05:53
  • @Henry Yes I have checked that it is the `NotFoundException` that is causing the issue. See my previous comment for how I was able to resolve the issue. – markvgti Jan 11 '19 at 05:56
  • @Henry I would be helpful if you voted to reopen the question. – markvgti Jan 11 '19 at 05:57
  • 1
    Actually it is a duplicate. – Henry Jan 11 '19 at 05:59

0 Answers0