/** * Closes all the provided closeables. * @throws IOException if any of the close methods throws an IOException. * The first IOException is thrown with subsequent exceptions * added as suppressed exceptions. */ public static void closeAll(Closeable... closeables) throws IOException { IOException exception = null; for (Closeable closeable : closeables) { try { if (closeable != null) closeable.close(); } catch (IOException e) { if (exception != null) exception.addSuppressed(e); else exception = e; } } if (exception != null) throw exception; }
/** * Attempts to move source to target atomically and falls back to a non-atomic move if it fails. * * @throws IOException if both atomic and non-atomic moves fail */ public static void atomicMoveWithFallback(Path source, Path target) throws IOException { try { Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); } catch (IOException outer) { try { Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); log.debug("Non-atomic move of {} to {} succeeded after atomic move failed due to {}", source, target, outer.getMessage()); } catch (IOException inner) { inner.addSuppressed(outer); throw inner; } } }
private static void close(final Closeable... closeables) throws IOException { IOException ioe = null; for (final Closeable closeable : closeables) { if (closeable == null) { continue; } try { closeable.close(); } catch (final IOException e) { if (ioe == null) { ioe = e; } else { ioe.addSuppressed(e); } } } if (ioe != null) { throw ioe; } }
Files.move(tmpPath, destPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e1) { e1.addSuppressed(e); LOGGER.log(Level.WARNING, "Unable to move {0} to {1}. Attempting to delete {0} and abandoning.", new Path[]{tmpPath, destPath}); Files.deleteIfExists(tmpPath); } catch (IOException e2) { e2.addSuppressed(e1); LOGGER.log(Level.WARNING, "Unable to delete {0}, good bye then!", tmpPath); throw e2;
private static void markAsFailed( DatabaseIndex<? extends IndexReader> index, IndexEntryConflictException conflict ) { try { index.markAsFailed( conflict.getMessage() ); } catch ( IOException ioe ) { ioe.addSuppressed( conflict ); throw new UncheckedIOException( ioe ); } }
private void forEachStream(final StreamProcessor proc) throws IOException { IOException exception = null; for (final OutputStream out : streamMap.values()) { try { proc.process(out); } catch (final IOException ioe) { if (exception == null) { exception = ioe; } else { ioe.addSuppressed(exception); exception = ioe; } } } if (exception != null) { throw exception; } }
close(); } catch (final IOException e) { ioe.addSuppressed(e);
/** * Opens all {@link Directory lucene directories} contained in the {@link #getIndexFolder() index folder}. * * @return the map from file system {@link File directory} to the corresponding {@link Directory lucene directory}. * @throws IOException if opening of some lucene directory (via {@link DirectoryFactory#open(File)}) fails. */ public Map<File,Directory> openIndexDirectories() throws IOException { Map<File,Directory> directories = new LinkedHashMap<>(); try { for ( File dir : listFolders() ) { directories.put( dir, directoryFactory.open( dir ) ); } } catch ( IOException oe ) { try { IOUtils.closeAll( directories.values() ); } catch ( Exception ce ) { oe.addSuppressed( ce ); } throw oe; } return directories; }
private void closeAndCollectExceptions( int channelIndex, IOException exception ) throws IOException { if ( channelIndex == channels.length ) { if ( exception != null ) { throw exception; } return; } try { channels[channelIndex].close(); } catch ( IOException e ) { if ( exception == null ) { exception = e; } else { exception.addSuppressed( e ); } } closeAndCollectExceptions( channelIndex + 1, exception ); }
/** * @deprecated Specific to {@link Mode#REMOTING}. */ @Deprecated /*package*/ CLI(CLIConnectionFactory factory) throws IOException, InterruptedException { URL jenkins = factory.jenkins; this.httpsProxyTunnel = factory.httpsProxyTunnel; this.authorization = factory.authorization; ExecutorService exec = factory.exec; ownsPool = exec==null; pool = exec!=null ? exec : Executors.newCachedThreadPool(new NamingThreadFactory(Executors.defaultThreadFactory(), "CLI.pool")); Channel _channel; try { _channel = connectViaCliPort(jenkins, getCliTcpPort(jenkins)); } catch (IOException e) { LOGGER.log(Level.FINE, "Failed to connect via CLI port. Falling back to HTTP", e); try { _channel = connectViaHttp(jenkins); } catch (IOException e2) { e.addSuppressed(e2); throw e; } } this.channel = _channel; // execute the command entryPoint = (CliEntryPoint)_channel.waitForRemoteProperty(CliEntryPoint.class.getName()); if(entryPoint.protocolVersion()!=CliEntryPoint.VERSION) throw new IOException(Messages.CLI_VersionMismatch()); }
static void test(final String[] args, final String config) { // System.out.println(System.getProperty("java.class.path")); try (final LoggerContext ctx = Configurator .initialize(ConsoleAppenderDefaultSuppressedThrowable.class.getName(), config)) { final IOException ioEx = new IOException("test suppressed"); ioEx.addSuppressed(new IOException("test suppressed 1", new IOException("test 1"))); final IOException ioEx2 = new IOException("test 2"); ioEx2.addSuppressed(new IOException("test 3")); ioEx.addSuppressed(new IOException("test suppressed 2", ioEx2)); final IOException e = new IOException("test", ioEx); LOG.error("Error message {}, suppressed?", "Hi", e); System.out.println("printStackTrace"); e.printStackTrace(); } }
/** * Note: Must be called while synchronizing on the MuninnPageCache instance. */ private void ensureThreadsInitialised() throws IOException { if ( threadsInitialised ) { return; } threadsInitialised = true; try { scheduler.schedule( Group.PAGE_CACHE, new EvictionTask( this ) ); } catch ( Exception e ) { IOException exception = new IOException( e ); try { close(); } catch ( Exception closeException ) { exception.addSuppressed( closeException ); } throw exception; } }
@Override public LineIterator next() { if (!hasNext()) { throw new NoSuchElementException(); } final OpenedObject<T> openedObject = fetcher.next(); try { return new ResourceCloseableLineIterator( new InputStreamReader( wrapObjectStream(openedObject.getObject(), openedObject.getObjectStream()), StandardCharsets.UTF_8 ), openedObject.getResourceCloser() ); } catch (IOException e) { try { openedObject.getResourceCloser().close(); } catch (Throwable t) { e.addSuppressed(t); } throw new RuntimeException(e); } } },
@Override public void run() { try { String line; while ( (line = in.readLine()) != null ) { if ( !quiet ) { out.write( prefix + line + "\n" ); out.flush(); } } } catch ( IOException exc ) { exc.addSuppressed( stackTraceOfOrigin ); failureHandler.handle( exc ); } } }
@Override public void cancel() throws IOException { if (mClosed) { return; } releaseCurrentChunk(); IOException exception = null; for (DataWriter dataWriter : mDataWriters) { try { dataWriter.cancel(); } catch (IOException e) { if (exception != null) { exception.addSuppressed(e); } } } if (exception != null) { throw exception; } close(); }
@Override public void terminateReads() throws IOException { if (tls) { try { sslEngine.closeInbound(); } catch (IOException ex) { try { super.terminateReads(); } catch (IOException e2) { e2.addSuppressed(ex); throw e2; } throw ex; } } super.terminateReads(); }
@Override public void terminateWrites() throws IOException { if (!tls) { super.terminateWrites(); return; } try { sslEngine.closeOutbound(); flush(); } catch (IOException e) { try { super.truncateWrites(); } catch (IOException e2) { e2.addSuppressed(e); throw e2; } throw e; } }
ioe.addSuppressed( e ); throw ioe;
} catch (ExecutionException x) { e.addSuppressed(x); throw e; } catch (TimeoutException ignored) {