0
votes

I'm trying to add a control when there is a connection error. My aim is to stop the program from throwing Exception by catching it.

try {
    Client client = ClientBuilder.newBuilder()
        .register(JacksonFeature.class)
        .build();
        client.register(new HttpBasicAuthFilter("login", "password"));
        WebTarget webTarget = client.target("http://path");
        Response response = webTarget.request().post(Entity.json(bean));
        System.out.println(response.getStatus());

} catch (Exception e) {
        // I do nothing here
}

But it prints in the console:

org.glassfish.jersey.client.ClientRequest writeEntity Grave: Error while committing the request output stream. java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at sun.net.NetworkClient.doConnect(NetworkClient.java:175) at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) at sun.net.www.http.HttpClient.(HttpClient.java:211) at sun.net.www.http.HttpClient.New(HttpClient.java:308) at sun.net.www.http.HttpClient.New(HttpClient.java:326) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091) at org.glassfish.jersey.client.HttpUrlConnector$3.getOutputStream(HttpUrlConnector.java:287) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:198) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:192) at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:260) at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:822) at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:451) at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290) at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203) at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:634) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:631) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:228) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:426) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:631) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:391) at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:300) at com.saintgobain.sg4p.igquality.desktop.DesktopClientController.sendToServer(DesktopClientController.java:857) at com.saintgobain.sg4p.igquality.desktop.DesktopClientController.handleSendToServerClicked(DesktopClientController.java:835) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)

If I add in the catch part: e.printStackTrace();

The console prints moreover:

Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at sun.net.NetworkClient.doConnect(NetworkClient.java:175) at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) at sun.net.www.http.HttpClient.(HttpClient.java:211) at sun.net.www.http.HttpClient.New(HttpClient.java:308) at sun.net.www.http.HttpClient.New(HttpClient.java:326) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091) at org.glassfish.jersey.client.HttpUrlConnector$3.getOutputStream(HttpUrlConnector.java:287) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:198) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:192) at org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:226) at org.codehaus.jackson.impl.Utf8Generator._flushBuffer(Utf8Generator.java:1754) at org.codehaus.jackson.impl.Utf8Generator.flush(Utf8Generator.java:1088) at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1615) at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:559) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:194) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139) at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005) at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430) at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290) at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203) ... 78 more

Sorry if I prints too much stacktrace...

I'd like to know if I don't print out the stacktrace, how can I make the exception disappear? Thanks :)

1

1 Answers

4
votes

It looks like your exception is logged by the framework as well as thrown to your client code. It may be either impossible to suppress it, or (if it is logged via a logging API) you could control it with an appropriate logging level configuration.