@Override public void run() { try { ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteStreams.copy(input, output); result = output.toString(Charset.defaultCharset().name()); } catch (Exception e) { result = Throwables.getStackTraceAsString(e); } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException} or {@link Error}. Example usage: * <pre> * try { * someMethodThatCouldThrowAnything(); * } catch (IKnowWhatToDoWithThisException e) { * handle(e); * } catch (Throwable t) { * Throwables.propagateIfPossible(t); * throw new RuntimeException("unexpected", t); * } * </pre> */ public static void propagateIfPossible(@Nullable Throwable throwable) { propagateIfInstanceOf(throwable, Error.class); propagateIfInstanceOf(throwable, RuntimeException.class); }
/** * Returns the Method that can be used to resolve an individual StackTraceElement, or null if that * method cannot be found (it is only to be found in fairly recent JDKs). */ @Nullable private static Method getGetMethod() { return getJlaMethod("getStackTraceElement", Throwable.class, int.class); }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, or {@code declaredType}. Example usage: * <pre> * try { * someMethodThatCouldThrowAnything(); * } catch (IKnowWhatToDoWithThisException e) { * handle(e); * } catch (Throwable t) { * Throwables.propagateIfPossible(t, OtherException.class); * throw new RuntimeException("unexpected", t); * } * </pre> * * @param throwable the Throwable to possibly propagate * @param declaredType the single checked exception type declared by the calling method */ public static <X extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X> declaredType) throws X { propagateIfInstanceOf(throwable, declaredType); propagateIfPossible(throwable); }
/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of either * of the given types. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. * <b>Note:</b> Be sure to declare all of the checked exception types your try block can throw * when calling an overload of this method so as to avoid losing the original exception type. * * <p>This method always throws, and as such should be called as * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException * @throws X1 when the given throwable is of the declared type X1 * @throws X2 when the given throwable is of the declared type X2 */ public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow( Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1, X2 { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); Throwables.propagateIfPossible(e, declaredType1, declaredType2); throw new RuntimeException(e); }
@CheckReturnValue public static List<StackTraceElement> lazyStackTrace(Throwable throwable) { return lazyStackTraceIsLazy() ? jlaStackTrace(throwable) : unmodifiableList(asList(throwable.getStackTrace()));
private static Object invokeAccessibleNonThrowingMethod( Method method, Object receiver, Object... params) { try { return method.invoke(receiver, params); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw propagate(e.getCause()); } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. In the * unlikely case that you have three or more declared checked exception types, you can handle them * all by invoking these methods repeatedly. See usage example in {@link * #propagateIfPossible(Throwable, Class)}. * * @param throwable the Throwable to possibly propagate * @param declaredType1 any checked exception type declared by the calling method * @param declaredType2 any other checked exception type declared by the calling method */ public static <X1 extends Throwable, X2 extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) throws X1, X2 { checkNotNull(declaredType2); propagateIfInstanceOf(throwable, declaredType1); propagateIfPossible(throwable, declaredType2); }
/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an * {@code IOException}, {@code RuntimeException}, {@code Error} or a checked exception of the * given type. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. <b>Note:</b> * Be sure to declare all of the checked exception types your try block can throw when calling an * overload of this method so as to avoid losing the original exception type. * * <p>This method always throws, and as such should be called as * {@code throw closer.rethrow(e, ...);} to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException * @throws X when the given throwable is of the declared type X */ public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, X { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); Throwables.propagateIfPossible(e, declaredType); throw new RuntimeException(e); }
/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an * {@code IOException}, {@code RuntimeException} or {@code Error}. Otherwise, it will be rethrown * wrapped in a {@code RuntimeException}. <b>Note:</b> Be sure to declare all of the checked * exception types your try block can throw when calling an overload of this method so as to avoid * losing the original exception type. * * <p>This method always throws, and as such should be called as * {@code throw closer.rethrow(e);} to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException */ public RuntimeException rethrow(Throwable e) throws IOException { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); throw new RuntimeException(e); }
/** * Default behavior of {@link Errors#dialog()} is @{link Throwable#printStackTrace()} * and {@link javax.swing.JOptionPane#showMessageDialog(java.awt.Component, Object, String, int) JOptionPane.showMessageDialog}. * * The `JOptionPane` part is called using reflection, and fails silently if swing isn't available. The `Throwable.printStackTrace` * part works whether swing is available or not. */ static void defaultDialog(Throwable error) { error.printStackTrace(); try { Method invokeLater = Class.forName("javax.swing.SwingUtilities").getMethod("invokeLater", Runnable.class); Class<?> javaAwtComponent = Class.forName("java.awt.Component"); Method showMessageDialog = Class.forName("javax.swing.JOptionPane").getMethod("showMessageDialog", javaAwtComponent, Object.class, String.class, int.class); Runnable runnable = () -> { try { String title = error.getClass().getSimpleName(); showMessageDialog.invoke(null, null, error.getMessage() + "\n\n" + Throwables.getStackTraceAsString(error), title, 0); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // if the reflection fails (e.g. because we're on Android) that's no problem, we already dumped the stacktrace to console } }; invokeLater.invoke(null, runnable); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { // if the reflection fails (e.g. because we're on Android) that's no problem, we already dumped the stacktrace to console } }
@SuppressWarnings("unchecked") // unsafe, which is one advantage of Cache over Map @Override public V get(Object key) { V value; try { value = getOrCompute((K) key); } catch (ExecutionException e) { Throwable cause = e.getCause(); Throwables.propagateIfInstanceOf(cause, ComputationException.class); throw new ComputationException(cause); } if (value == null) { throw new NullPointerException(computingFunction + " returned null for key " + key + "."); } return value; } }
/** * Returns the Method that can be used to return the size of a stack, or null if that method * cannot be found (it is only to be found in fairly recent JDKs). */ @Nullable private static Method getSizeMethod() { return getJlaMethod("getStackTraceDepth", Throwable.class); }
/** * Propagates {@code throwable} as-is if it is an instance of {@link RuntimeException} or {@link * Error}, or else as a last resort, wraps it in a {@code RuntimeException} and then propagates. * <p> * This method always throws an exception. The {@code RuntimeException} return type is only for * client code to make Java type system happy in case a return value is required by the enclosing * method. Example usage: * <pre> * T doSomething() { * try { * return someMethodThatCouldThrowAnything(); * } catch (IKnowWhatToDoWithThisException e) { * return handle(e); * } catch (Throwable t) { * throw Throwables.propagate(t); * } * } * </pre> * * @param throwable the Throwable to propagate * @return nothing will ever be returned; this return type is only for your convenience, as * illustrated in the example above */ public static RuntimeException propagate(Throwable throwable) { propagateIfPossible(checkNotNull(throwable)); throw new RuntimeException(throwable); }
/** * Closes all {@code Closeable} instances that have been added to this {@code Closer}. If an * exception was thrown in the try block and passed to one of the {@code exceptionThrown} methods, * any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the * <i>first</i> exception to be thrown from an attempt to close a closeable will be thrown and any * additional exceptions that are thrown after that will be suppressed. */ @Override public void close() throws IOException { Throwable throwable = thrown; // close closeables in LIFO order while (!stack.isEmpty()) { Closeable closeable = stack.removeFirst(); try { closeable.close(); } catch (Throwable e) { if (throwable == null) { throwable = e; } else { suppressor.suppress(closeable, throwable, e); } } } if (thrown == null && throwable != null) { Throwables.propagateIfPossible(throwable, IOException.class); throw new AssertionError(throwable); // not possible } }