IDEA-130959 (exceptions passing)
authorRoman Shevchenko <roman.shevchenko@jetbrains.com>
Tue, 11 Nov 2014 17:10:21 +0000 (18:10 +0100)
committerRoman Shevchenko <roman.shevchenko@jetbrains.com>
Tue, 11 Nov 2014 17:10:53 +0000 (18:10 +0100)
platform/core-impl/src/com/intellij/openapi/application/ex/ApplicationUtil.java
platform/core-impl/src/com/intellij/openapi/fileEditor/impl/LoadTextUtil.java
platform/util/src/com/intellij/util/ExceptionUtil.java

index 0022be7759ad53528f77993514268ae88374b6c9..94061b31aceef1cc1b860457721ba75767fbf2be 100644 (file)
@@ -20,6 +20,7 @@ import com.intellij.openapi.progress.ProcessCanceledException;
 import com.intellij.openapi.progress.ProgressManager;
 import com.intellij.openapi.util.Computable;
 import com.intellij.openapi.util.Ref;
+import com.intellij.util.ExceptionUtil;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.concurrent.*;
@@ -49,8 +50,21 @@ public class ApplicationUtil {
    * Allows to interrupt a process which does not performs checkCancelled() calls by itself.
    * Note that the process may continue to run in background indefinitely - so <b>avoid using this method unless absolutely needed</b>.
    */
-  public static <T> T runWithCheckCanceled(@NotNull Callable<T> callable) throws ExecutionException, InterruptedException {
-    Future<T> future = ApplicationManager.getApplication().executeOnPooledThread(callable);
+  public static <T> T runWithCheckCanceled(@NotNull final Callable<T> callable) throws Exception {
+    final Ref<Throwable> error = Ref.create();
+
+    Future<T> future = ApplicationManager.getApplication().executeOnPooledThread(new Callable<T>() {
+      @Override
+      public T call() throws Exception {
+        try {
+          return callable.call();
+        }
+        catch (Throwable t) {
+          error.set(t);
+          return null;
+        }
+      };
+    });
 
     while (true) {
       try {
@@ -62,7 +76,9 @@ public class ApplicationUtil {
       }
 
       try {
-        return future.get(200, TimeUnit.MILLISECONDS);
+        T result = future.get(200, TimeUnit.MILLISECONDS);
+        ExceptionUtil.rethrowAll(error.get());
+        return result;
       }
       catch (TimeoutException ignored) { }
     }
index 9baa23527b25e0f5ca1e8f4affcb69c95e70290f..bf4c421c38ea6ef896d14086253344d8d2026faa 100644 (file)
@@ -37,6 +37,7 @@ import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.encoding.EncodingRegistry;
 import com.intellij.testFramework.LightVirtualFile;
 import com.intellij.util.ArrayUtil;
+import com.intellij.util.ExceptionUtil;
 import com.intellij.util.text.CharArrayUtil;
 import org.jetbrains.annotations.Nls;
 import org.jetbrains.annotations.NotNull;
@@ -374,11 +375,10 @@ public final class LoadTextUtil {
       if (decompiler != null) {
         CharSequence text;
 
-        final Application app = ApplicationManager.getApplication();
+        Application app = ApplicationManager.getApplication();
         if (app != null && app.isDispatchThread() && !app.isWriteAccessAllowed()) {
           final Ref<CharSequence> result = Ref.create(ArrayUtil.EMPTY_CHAR_SEQUENCE);
-          final Ref<RuntimeException> exception = Ref.create();
-
+          final Ref<Throwable> error = Ref.create();
           ProgressManager.getInstance().run(new Task.Modal(null, "Decompiling " + file.getName(), true) {
             @Override
             public void run(@NotNull ProgressIndicator indicator) {
@@ -391,17 +391,12 @@ public final class LoadTextUtil {
                   }
                 }));
               }
-              catch (RuntimeException e) {
-                exception.set(e);
+              catch (Throwable t) {
+                error.set(t);
               }
-              catch (Exception ignored) { }
             }
           });
-
-          if (!exception.isNull()) {
-            throw exception.get();
-          }
-
+          ExceptionUtil.rethrowUnchecked(error.get());
           text = result.get();
         }
         else {
index d22d122ad2b231f402acbe1fc4813d33320f9978..cc7b67ea805ff0d79909c7925c0ddac48365d4a1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2014 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -146,4 +146,19 @@ public class ExceptionUtil {
     }
     return result;
   }
+
+  public static void rethrowUnchecked(@Nullable Throwable t) {
+    if (t != null) {
+      if (t instanceof Error) throw (Error)t;
+      if (t instanceof RuntimeException) throw (RuntimeException)t;
+    }
+  }
+
+  public static void rethrowAll(@Nullable Throwable t) throws Exception {
+    if (t != null) {
+      if (t instanceof Error) throw (Error)t;
+      if (t instanceof RuntimeException) throw (RuntimeException)t;
+      throw (Exception)t;
+    }
+  }
 }