Example usage for java.lang Throwable addSuppressed

List of usage examples for java.lang Throwable addSuppressed

Introduction

In this page you can find the example usage for java.lang Throwable addSuppressed.

Prototype

public final synchronized void addSuppressed(Throwable exception) 

Source Link

Document

Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.

Usage

From source file:Main.java

public static void main(String[] argv) {
    Throwable t = new Exception("from java2s.com");
    t.addSuppressed(new Throwable("from java2s.com"));

}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

public static void printJsonError(Throwable throwable) {
    String error;/*from  w  w w. j  a  v  a  2s .c o  m*/
    try {
        error = putError(new JSONObject(), throwable).toString(4);
    } catch (JSONException e) {
        throwable.addSuppressed(e);
        error = putError(new JSONObject(), throwable).toString();
    }
    OUT.print(error);
}

From source file:alluxio.job.move.MoveDefinition.java

/**
 * @param command the move command to execute
 * @param writeType the write type to use for the moved file
 * @param fileSystem the Alluxio file system
 *///  w w w. j a va 2s  .c  om
private static void move(MoveCommand command, WriteType writeType, FileSystem fileSystem) throws Exception {
    String source = command.getSource();
    String destination = command.getDestination();
    LOG.debug("Moving {} to {}", source, destination);

    try (FileOutStream out = fileSystem.createFile(new AlluxioURI(destination),
            CreateFileOptions.defaults().setWriteType(writeType))) {
        try (FileInStream in = fileSystem.openFile(new AlluxioURI(source))) {
            IOUtils.copy(in, out);
        } catch (Throwable t) {
            try {
                out.cancel();
            } catch (Throwable t2) {
                t.addSuppressed(t2);
            }
            throw t;
        }
    }
    fileSystem.delete(new AlluxioURI(source));
}

From source file:com.spotify.docker.client.CompressedDirectory.java

/**
 * This method creates a gzip tarball of the specified directory. File permissions will be
 * retained. The file will be created in a temporary directory using the {@link
 * Files#createTempFile(String, String, FileAttribute[])} method. The returned object is
 * auto-closeable, and upon closing it, the archive file will be deleted.
 *
 * @param directory the directory to compress
 * @return a Path object representing the compressed directory
 * @throws IOException if the compressed directory could not be created.
 *///from  ww w .  ja  v a  2  s.co  m
public static CompressedDirectory create(final Path directory) throws IOException {
    final Path file = Files.createTempFile("docker-client-", ".tar.gz");

    final Path dockerIgnorePath = directory.resolve(".dockerignore");
    final ImmutableSet<PathMatcher> ignoreMatchers = parseDockerIgnore(dockerIgnorePath);

    try (final OutputStream fileOut = Files.newOutputStream(file);
            final GzipCompressorOutputStream gzipOut = new GzipCompressorOutputStream(fileOut);
            final TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gzipOut)) {
        tarOut.setLongFileMode(LONGFILE_POSIX);
        tarOut.setBigNumberMode(BIGNUMBER_POSIX);
        Files.walkFileTree(directory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new Visitor(directory, ignoreMatchers, tarOut));

    } catch (Throwable t) {
        // If an error occurs, delete temporary file before rethrowing exception.
        try {
            Files.delete(file);
        } catch (IOException e) {
            // So we don't lose track of the reason the file was deleted... might be important
            t.addSuppressed(e);
        }

        throw t;
    }

    return new CompressedDirectory(file);
}

From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java

/**
 * Function used to wait for a service to be available.
 *
 * @param host/*from   ww  w.  ja  va  2 s . c om*/
 * @param timeout
 * @param serviceLinks
 * @throws Throwable
 */
public static void waitForServiceAvailability(ServiceHost host, long timeout, String... serviceLinks)
        throws Throwable {
    final CountDownLatch latch = new CountDownLatch(serviceLinks.length);
    final Throwable error = new Throwable("Error: registerForAvailability returned errors");

    Operation.CompletionHandler handler = new Operation.CompletionHandler() {
        @Override
        public void handle(Operation operation, Throwable throwable) {
            if (null != throwable) {
                error.addSuppressed(throwable);
            }

            latch.countDown();
        }
    };
    host.registerForServiceAvailability(handler, serviceLinks);

    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw new TimeoutException(
                String.format("One or several of service(s) %s not available", Utils.toJson(serviceLinks)));
    }

    if (error.getSuppressed().length > 0) {
        throw error;
    }
}

From source file:com.vmware.photon.controller.common.xenon.ServiceHostUtils.java

/**
 * Function used to wait for a service to be available.
 *
 * @param host/*  w  w w.j a v a  2  s.  co  m*/
 * @param timeout
 * @param serviceLinks
 * @throws Throwable
 */
public static void waitForServiceAvailability(ServiceHost host, long timeout, String... serviceLinks)
        throws Throwable {
    final CountDownLatch latch = new CountDownLatch(serviceLinks.length);
    final Throwable error = new Throwable("Error: registerForAvailability returned errors");

    Operation.CompletionHandler handler = new Operation.CompletionHandler() {
        @Override
        public void handle(Operation operation, Throwable throwable) {
            if (null != throwable) {
                error.addSuppressed(throwable);
            }

            latch.countDown();
        }
    };
    host.registerForServiceAvailability(handler, serviceLinks);

    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw new TimeoutException(String.format("One or several of service(s) %s not available",
                Utils.toJson(false, false, serviceLinks)));
    }

    if (error.getSuppressed().length > 0) {
        throw error;
    }
}

From source file:fi.jumi.core.api.StackTraceTest.java

@Test
public void suppressed_exceptions_are_also_copied_recursively() {
    Throwable original = new Throwable("original message");
    original.addSuppressed(new Exception("suppressed 1"));
    original.addSuppressed(new RuntimeException("suppressed 2"));

    StackTrace copy = StackTrace.from(original);

    Throwable[] suppressed = copy.getSuppressed();
    assertThat(suppressed.length, is(2));
    assertThat(suppressed[0], is(instanceOf(StackTrace.class)));
    assertThat(suppressed[0].getMessage(), is("suppressed 1"));
    assertThat(suppressed[1], is(instanceOf(StackTrace.class)));
    assertThat(suppressed[1].getMessage(), is("suppressed 2"));
}

From source file:org.bigtester.ate.systemlogger.GenericTestCaseLogger.java

/**
 * Sets the already case point cut./*  w w w.j a va2s. com*/
 *
 * @param error
 *            the new already case point cut
 */
private void setAlreadyCasePointCut(Throwable error) {
    Throwable flagT = new Exception(ExceptionMessage.MSG_ALREADY_CASEPOINTCUT);
    error.addSuppressed(flagT);
}

From source file:org.grouplens.grapht.LifecycleManager.java

/**
 * Close the lifecycle manager, shutting down all components it manages.
 *//*ww  w.  j a va  2s  . co  m*/
@Override
public void close() {
    Throwable error = null;
    while (!actions.isEmpty()) {
        TeardownAction action = actions.removeFirst();
        try {
            action.destroy();
        } catch (Throwable th) {
            if (error == null) {
                error = th;
            } else {
                error.addSuppressed(th);
            }
        }
    }
    if (error != null) {
        throw Throwables.propagate(error);
    }
}

From source file:org.elasticsearch.ExceptionsHelperTests.java

public void testMaybeError() {
    final Error outOfMemoryError = new OutOfMemoryError();
    assertError(outOfMemoryError, outOfMemoryError);

    final DecoderException decoderException = new DecoderException(outOfMemoryError);
    assertError(decoderException, outOfMemoryError);

    final Exception e = new Exception();
    e.addSuppressed(decoderException);//from w  w  w.j a  va2 s  . c  o m
    assertError(e, outOfMemoryError);

    final int depth = randomIntBetween(1, 16);
    Throwable cause = new Exception();
    boolean fatal = false;
    Error error = null;
    for (int i = 0; i < depth; i++) {
        final int length = randomIntBetween(1, 4);
        for (int j = 0; j < length; j++) {
            if (!fatal && rarely()) {
                error = new Error();
                cause.addSuppressed(error);
                fatal = true;
            } else {
                cause.addSuppressed(new Exception());
            }
        }
        if (!fatal && rarely()) {
            cause = error = new Error(cause);
            fatal = true;
        } else {
            cause = new Exception(cause);
        }
    }
    if (fatal) {
        assertError(cause, error);
    } else {
        assertFalse(maybeError(cause, logger).isPresent());
    }

    assertFalse(maybeError(new Exception(new DecoderException()), logger).isPresent());

    Throwable chain = outOfMemoryError;
    for (int i = 0; i < MAX_ITERATIONS; i++) {
        chain = new Exception(chain);
    }
    assertFalse(maybeError(chain, logger).isPresent());
}