# HG changeset patch
# Parent  1e6c654b157ebc157769f91c86356a2504e5442c

diff -r 1e6c654b157e -r 3e7b4c833947 src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Jul 22 18:43:51 2020 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Jul 22 20:43:49 2020 -0400
@@ -97,13 +97,18 @@
     /* Internal version of call exposing Main.Result. */
     public Main.Result doCall() {
         try {
-            return handleExceptions(() -> {
+            Pair<Main.Result, Throwable> result = invocationHelper(() -> {
                 prepareCompiler(false);
                 if (compiler.errorCount() > 0)
                     return Main.Result.ERROR;
                 compiler.compile(args.getFileObjects(), args.getClassNames(), processors, addModules);
                 return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME?
-            }, Main.Result.SYSERR, Main.Result.ABNORMAL);
+            });
+            if (result.snd == null) {
+                return result.fst;
+            } else {
+                return (result.snd instanceof FatalError) ? Main.Result.SYSERR : Main.Result.ABNORMAL;
+            }
         } finally {
             try {
                 cleanup();
@@ -141,10 +146,10 @@
         this.locale = locale;
     }
 
-    private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorResult) {
+    private <T> Pair<T, Throwable> invocationHelper(Callable<T> c) {
         Handler prevDeferredHandler = dcfh.setHandler(dcfh.javacCodeHandler);
         try {
-            return c.call();
+            return new Pair<>(c.call(), null);
         } catch (FatalError ex) {
             Log log = Log.instance(context);
             Options options = Options.instance(context);
@@ -152,7 +157,7 @@
             if (ex.getCause() != null && options.isSet("dev")) {
                 ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
             }
-            return sysErrorResult;
+            return new Pair<>(null, ex);
         } catch (AnnotationProcessingError | ClientCodeException e) {
             // AnnotationProcessingError is thrown from JavacProcessingEnvironment,
             // to forward errors thrown from an annotation processor
@@ -175,7 +180,7 @@
                 log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
                 ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
             }
-            return abnormalErrorResult;
+            return new Pair<>(null, ex);
         } finally {
             dcfh.setHandler(prevDeferredHandler);
         }
@@ -240,7 +245,11 @@
 
     @Override @DefinedBy(Api.COMPILER_TREE)
     public Iterable<? extends CompilationUnitTree> parse() {
-        return handleExceptions(this::parseInternal, List.nil(), List.nil());
+        Pair<Iterable<? extends CompilationUnitTree>, Throwable> result =  invocationHelper(this::parseInternal);
+        if (result.snd == null) {
+            return result.fst;
+        }
+        throw new IllegalStateException(result.snd);
     }
 
     private Iterable<? extends CompilationUnitTree> parseInternal() {
@@ -367,7 +376,11 @@
 
     @Override @DefinedBy(Api.COMPILER_TREE)
     public Iterable<? extends Element> analyze() {
-        return handleExceptions(() -> analyze(null), List.nil(), List.nil());
+        Pair<Iterable<? extends Element>, Throwable> result =  invocationHelper(() -> analyze(null));
+        if (result.snd == null) {
+            return result.fst;
+        }
+        throw new IllegalStateException(result.snd);
     }
 
     /**
@@ -429,7 +442,11 @@
 
     @Override @DefinedBy(Api.COMPILER_TREE)
     public Iterable<? extends JavaFileObject> generate() {
-        return handleExceptions(() -> generate(null), List.nil(), List.nil());
+        Pair<Iterable<? extends JavaFileObject>, Throwable> result =  invocationHelper(() -> generate(null));
+        if (result.snd == null) {
+            return result.fst;
+        }
+        throw new IllegalStateException(result.snd);
     }
 
     /**
