private void runCallbacks() { RuntimeException e = null; for (Runnable callback = shutdownCallbacks.poll(); callback != null; callback = shutdownCallbacks.poll()) { try { callback.run(); } catch (RuntimeException ex) { if (e == null) { e = new RuntimeException("Error running callback"); } e.addSuppressed(ex); } } if (e != null) { throw e; } }
/** * If the given obj is AutoCloseable, then close it. * Any exceptions thrown from the close method will be wrapped in RuntimeExceptions. * These RuntimeExceptions can get the given suppressedException attached to them, if any. * If the given suppressedException argument is null, then it will not be added to the * thrown RuntimeException. */ static void closeSafely( Object obj, Throwable suppressedException ) { if ( obj instanceof AutoCloseable ) { AutoCloseable closeable = (AutoCloseable) obj; try { closeable.close(); } catch ( Exception cause ) { RuntimeException exception = new RuntimeException( cause ); if ( suppressedException != null ) { exception.addSuppressed( suppressedException ); } throw exception; } } } }
/** * If the given obj is AutoCloseable, then close it. * Any exceptions thrown from the close method will be wrapped in RuntimeExceptions. * These RuntimeExceptions can get the given suppressedException attached to them, if any. * If the given suppressedException argument is null, then it will not be added to the * thrown RuntimeException. */ static void closeSafely( Object obj, Throwable suppressedException ) { if ( obj instanceof AutoCloseable ) { AutoCloseable closeable = (AutoCloseable) obj; try { closeable.close(); } catch ( Exception cause ) { RuntimeException exception = new RuntimeException( cause ); if ( suppressedException != null ) { exception.addSuppressed( suppressedException ); } throw exception; } } } }
/** * Create a composite exception that wraps the given {@link Throwable Throwable(s)}, * as suppressed exceptions. Instances create by this method can be detected using the * {@link #isMultiple(Throwable)} check. The {@link #unwrapMultiple(Throwable)} method * will correctly unwrap these to a {@link List} of the suppressed exceptions. Note * that is will also be consistent in producing a List for other types of exceptions * by putting the input inside a single-element List. * * @param throwables the exceptions to wrap into a composite * @return a composite exception with a standard message, and the given throwables as * suppressed exceptions * @see #addThrowable(AtomicReferenceFieldUpdater, Object, Throwable) */ public static RuntimeException multiple(Iterable<Throwable> throwables) { RuntimeException multiple = new RuntimeException("Multiple exceptions"); //noinspection ConstantConditions if (throwables != null) { for (Throwable t : throwables) { //this is ok, multiple is always a new non-singleton instance multiple.addSuppressed(t); } } return multiple; }
@Override public void close() { if (closed) { return; } RuntimeException saved = null; for (Iterator<String> it = super.iterator(); it.hasNext();) { String name = it.next(); try { client.killTopologyWithOpts(name, NO_WAIT_KILL); it.remove(); } catch (Exception e) { RuntimeException wrapped = new RuntimeException("Error trying to kill " + name, e); if (saved != null) { saved.addSuppressed(wrapped); } else { saved = wrapped; } } } super.clear(); if (saved != null) { throw saved; } closed = true; } }
public RuntimeException getRepresentativeException() { RuntimeException top = null; for (Throwable ex : getExceptions()) { if (top != null) { top.addSuppressed(ex); } else { if (ex instanceof RuntimeException) { top = (RuntimeException) ex; } else { top = new RuntimeException(ex); } } } if (top == null) { top = new RuntimeException("Some transactions are not committed"); } return top; }
@Override public void close() throws Exception { RuntimeException previousException = null; try { closeHandler.run(); } catch (RuntimeException e) { previousException = e; } try { innerStream.close(); } catch (RuntimeException e) { if (previousException != null) { e.addSuppressed(previousException); } throw e; } } }
@Override public void close() throws Exception { RuntimeException previousException = null; try { closeHandler.run(); } catch (RuntimeException e) { previousException = e; } try { innerStream.close(); } catch (RuntimeException e) { if (previousException != null) { e.addSuppressed(previousException); } throw e; } } }
@Override public void close() throws Exception { RuntimeException previousException = null; try { closeHandler.run(); } catch (RuntimeException e) { previousException = e; } try { innerStream.close(); } catch (RuntimeException e) { if (previousException != null) { e.addSuppressed(previousException); } throw e; } } }
@Override public void close() throws Exception { RuntimeException previousException = null; try { closeHandler.run(); } catch (RuntimeException e) { previousException = e; } try { innerStream.close(); } catch (RuntimeException e) { if (previousException != null) { e.addSuppressed(previousException); } throw e; } } }
e.addSuppressed(e2); throw e;
@Override public void close() throws Exception { RuntimeException suppressed = null; try { first.close(); } catch (RuntimeException e) { suppressed = e; } try { second.close(); } catch (RuntimeException e) { if (suppressed != null) { e.addSuppressed(suppressed); } throw e; } } }
@Override public void close() throws Exception { RuntimeException suppressed = null; try { first.close(); } catch (RuntimeException e) { suppressed = e; } try { second.close(); } catch (RuntimeException e) { if (suppressed != null) { e.addSuppressed(suppressed); } throw e; } } }
@Override public void close() throws Exception { RuntimeException suppressed = null; try { first.close(); } catch (RuntimeException e) { suppressed = e; } try { second.close(); } catch (RuntimeException e) { if (suppressed != null) { e.addSuppressed(suppressed); } throw e; } } }
@Override public void close() throws Exception { RuntimeException suppressed = null; try { first.close(); } catch (RuntimeException e) { suppressed = e; } try { second.close(); } catch (RuntimeException e) { if (suppressed != null) { e.addSuppressed(suppressed); } throw e; } } }
@Override public void close() { RuntimeException exception = null; for ( Pair<SnapshotDeletionPolicy,IndexCommit> policyAndCommit : snapshots ) { try { policyAndCommit.first().release( policyAndCommit.other() ); } catch ( IOException | RuntimeException e ) { if ( exception == null ) { exception = e instanceof IOException ? new UncheckedIOException( (IOException) e ) : (RuntimeException) e; } else { exception.addSuppressed( e ); } } } if ( exception != null ) { throw exception; } } };
RuntimeException exception = new RuntimeException(message.toString(), iterator.next()); while (iterator.hasNext()) { exception.addSuppressed(iterator.next());
public static void cleanup(Job job) throws IOException { final Path jobDir = getJobPath(job.getJobID(), job.getWorkingDirectory()); final FileSystem fs = jobDir.getFileSystem(job.getConfiguration()); RuntimeException e = null; try { JobHelper.deleteWithRetry(fs, jobDir, true); } catch (RuntimeException ex) { e = ex; } try { JobHelper.deleteWithRetry(fs, getJobClassPathDir(job.getJobName(), job.getWorkingDirectory()), true); } catch (RuntimeException ex) { if (e == null) { e = ex; } else { e.addSuppressed(ex); } } if (e != null) { throw e; } }
/** * @throws Exception If failed. */ @Test public void testXGetSuppressedList() throws Exception { IgniteCheckedException me = prepareMultiException(); assertEquals(3, X.getSuppressedList(me).size()); RuntimeException e = new RuntimeException(); e.addSuppressed(me); List<Throwable> suppresseds = X.getSuppressedList(e); assertEquals(4, suppresseds.size()); assertEquals("Test message.", suppresseds.get(0).getMessage()); for (int i = 1; i <= 3; i++) assertEquals("Demo exception.", suppresseds.get(1).getMessage()); }
initException.addSuppressed( closeException );