Example usage for java.lang Exception getSuppressed

List of usage examples for java.lang Exception getSuppressed

Introduction

In this page you can find the example usage for java.lang Exception getSuppressed.

Prototype

public final synchronized Throwable[] getSuppressed() 

Source Link

Document

Returns an array containing all of the exceptions that were suppressed, typically by the try -with-resources statement, in order to deliver this exception.

Usage

From source file:Test.java

public static void main(String[] args) {
    try (MyResource resource1 = new MyResource(); OtherResource resource2 = new OtherResource()) {
        resource1.do1();
        resource2.do2();
    } catch (Exception e) {
        e.printStackTrace();/* w w w .jav  a 2s.  c  o m*/
        for (Throwable throwable : e.getSuppressed()) {
            System.out.println(throwable);
        }
    }

}

From source file:com.vrane.metaGlacierSDK.MD5File.java

String md5hex() {
    try (FileInputStream in = new FileInputStream(path)) {
        return DigestUtils.md5Hex(in);
    } catch (FileNotFoundException ex) {
        LGR.log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        LGR.log(Level.SEVERE, null, ex);
    } catch (Exception e) {
        if (e.getSuppressed() != null) {
            for (Throwable t : e.getSuppressed()) {
                LGR.log(Level.SEVERE, null, t);
            }/*from   w  ww .j a v a 2 s  .c o m*/
        } else {
            LGR.log(Level.SEVERE, null, e);
        }
    }
    return null;
}

From source file:net.orfjackal.retrolambda.test.TryWithResourcesTest.java

@Test
public void suppressed_exceptions() {
    Exception thrown;
    try {// w  ww .  j a  v a 2  s  .co m
        try (ThrowSecondaryExceptionOnClose c = new ThrowSecondaryExceptionOnClose()) {
            throw new PrimaryException();
        }
    } catch (Exception e) {
        thrown = e;
    }

    assertThat("thrown", thrown, is(instanceOf(PrimaryException.class)));
    assertThat("cause", thrown.getCause(), is(nullValue()));

    // On Java 6 and lower we will swallow the suppressed exception, because the API does not exist,
    // but on Java 7 we want to keep the original behavior.
    if (SystemUtils.isJavaVersionAtLeast(1.7f)) {
        assertThat("suppressed", thrown.getSuppressed(), arrayContaining(instanceOf(SecondaryException.class)));
    }
}

From source file:de.haber.xmind2latex.XMindToLatexExporterConfigurationTest.java

@Test
public void testConfigureInXMindSource() {
    File in = new File("src/test/resources/example.xmind");

    String expectedOut = in.getAbsolutePath().concat(".tex");

    String[] args = new String[] { "-i", in.getPath() };
    XMindToLatexExporter exporter;// w ww  .  j  ava2s.  c  om
    try {
        exporter = CliParameters.build(args);
        assertEquals(expectedOut, exporter.getTargetFile().getAbsolutePath());
        assertTrue(exporter.getxMindSourceAsStream().getClass().getName(),
                exporter.getxMindSourceAsStream() instanceof ZipInputStream);
        ZipInputStream fis = (ZipInputStream) exporter.getxMindSourceAsStream();
        assertNotNull(fis);
        assertFalse(exporter.isOverwriteExistingFile());
    } catch (Exception e) {
        for (Throwable t : e.getSuppressed()) {
            t.printStackTrace();
        }
        fail(e.getMessage());
    }
}

From source file:com.strandls.alchemy.rest.client.AlchemyRestClientFactoryTest.java

/**
 * Test method for/*from   w  w  w  . j  av a 2 s  .  c o m*/
 * {@link com.strandls.alchemy.rest.client.AlchemyRestClientFactory#getInstance(java.lang.Class, java.lang.String, javax.ws.rs.client.Client)}
 * .
 *
 * Test generic exception is relayed correctly without loosing message.
 *
 * @throws Exception
 */
@Test
public void testExceptionMapping() throws Exception {
    final TestWebserviceExceptionHandling service = clientFactory
            .getInstance(TestWebserviceExceptionHandling.class);
    try {
        service.fail();
        fail("Should have thrown an exception");
    } catch (final Exception e) {
        assertEquals(TestWebserviceExceptionHandling.EXCEPTION_STRING, e.getMessage());

        // ensure the mixin got applied
        assertNull(e.getCause());
        // ensure server stack trace is masked.
        // will have local stack trace though.
        final StackTraceElement[] stackTrace = e.getStackTrace();
        for (final StackTraceElement stackTraceElement : stackTrace) {
            assertFalse(stackTraceElement.toString()
                    .contains(TestWebserviceExceptionHandling.class.getName() + ".fail"));
        }
        assertEquals(0, e.getSuppressed().length);

    }

}

From source file:org.sakuli.services.forwarder.gearman.GearmanResultServiceImplTest.java

@Test
public void testSaveAllResultsWrongConnectionCacheResults() throws Exception {
    when(properties.getServiceType()).thenReturn("passive");
    final String queueName = "check_results";
    when(properties.getServerQueue()).thenReturn(queueName);
    final String host = "not-resolveable-host.de";
    when(properties.getServerHost()).thenReturn(host);
    final int port = 4730;
    when(properties.getServerPort()).thenReturn(port);
    when(properties.getNagiosHost()).thenReturn("win7sakuli");
    when(properties.isCacheEnabled()).thenReturn(true);

    when(checkResultBuilder.build()).thenReturn(
            new NagiosCachedCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult));

    GearmanClient gearmanClient = mock(GearmanClientImpl.class);
    doReturn(gearmanClient).when(testling).getGearmanClient();
    GearmanJobServerConnection connection = mock(GearmanJobServerConnection.class);
    doReturn(connection).when(testling).getGearmanConnection(host, port);
    when(gearmanClient.addJobServer(connection)).thenThrow(new UnresolvedAddressException());

    when(gearmanCacheService.getCachedResults()).thenReturn(Collections.emptyList());

    doAnswer(invocationOnMock -> {/*from   www .  j ava 2 s .  c  o m*/
        assertEquals(((List) invocationOnMock.getArguments()[0]).size(), 1L);
        return null;
    }).when(gearmanCacheService).cacheResults(anyList());

    doAnswer(invocationOnMock -> {
        Exception exception = (Exception) invocationOnMock.getArguments()[0];
        assertRegExMatch(exception.getMessage(), "Could not transfer Sakuli results to the Gearman server.*");
        assertEquals(exception.getSuppressed()[0].getClass(), UnresolvedAddressException.class);
        return null;
    }).when(exceptionHandler).handleException(any(Throwable.class), anyBoolean());

    testling.saveAllResults();

    //checks
    verify(gearmanCacheService).cacheResults(anyList());
    verify(gearmanCacheService).getCachedResults();
    verify(exceptionHandler).handleException(any(SakuliForwarderException.class), anyBoolean());
    verify(testling).getGearmanClient();
    verify(testling).getGearmanConnection(host, port);
    verify(gearmanClient).addJobServer(connection);
    verify(gearmanClient).shutdown();
}