为什么程序仍然抛出java.net.ConnectException甚至我添加了try..catch?

时间:2013-09-20 09:20:24

标签: java exception connection jersey-client

我尝试在出现连接错误时添加控件。我的目标是通过捕获它来阻止程序抛出异常。

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
}

但是它会在控制台中打印出来:

  

org.glassfish.jersey.client.ClientRequest writeEntity   严重:提交请求输出流时出错。   java.net.ConnectException:连接被拒绝:连接       at java.net.DualStackPlainSocketImpl.connect0(Native Method)       在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)       在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)       在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)       在java.net.Socket.connect(Socket.java:579)       在sun.net.NetworkClient.doConnect(NetworkClient.java:175)       在sun.net.www.http.HttpClient.openServer(HttpClient.java:432)       在sun.net.www.http.HttpClient.openServer(HttpClient.java:527)       在sun.net.www.http.HttpClient。(HttpClient.java:211)       在sun.net.www.http.HttpClient.New(HttpClient.java:308)       在sun.net.www.http.HttpClient.New(HttpClient.java:326)       在sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)       at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)       在sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)       at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091)       在org.glassfish.jersey.client.HttpUrlConnector $ 3.getOutputStream(HttpUrlConnector.java:287)       在org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:198)       在org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:192)       在org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:260)       在org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:822)       在org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:451)       在org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290)       在org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203)       在org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)       在org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:634)       在org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:631)       在org.glassfish.jersey.internal.Errors.process(Errors.java:315)       在org.glassfish.jersey.internal.Errors.process(Errors.java:297)       在org.glassfish.jersey.internal.Errors.process(Errors.java:228)       在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:426)       在org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:631)       在org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:391)       在org.glassfish.jersey.client.JerseyInvocation $ Builder.post(JerseyInvocation.java:300)       在com.saintgobain.sg4p.igquality.desktop.DesktopClientController.sendToServer(DesktopClientController.java:857)       在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)

如果我添加了catch部分:e.printStackTrace();

控制台还打印:

  

引起:java.net.ConnectException:连接被拒绝:连接       at java.net.DualStackPlainSocketImpl.connect0(Native Method)       在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)       在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)       在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)       在java.net.Socket.connect(Socket.java:579)       在sun.net.NetworkClient.doConnect(NetworkClient.java:175)       在sun.net.www.http.HttpClient.openServer(HttpClient.java:432)       在sun.net.www.http.HttpClient.openServer(HttpClient.java:527)       在sun.net.www.http.HttpClient。(HttpClient.java:211)       在sun.net.www.http.HttpClient.New(HttpClient.java:308)       在sun.net.www.http.HttpClient.New(HttpClient.java:326)       在sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)       at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)       在sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)       at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091)       在org.glassfish.jersey.client.HttpUrlConnector $ 3.getOutputStream(HttpUrlConnector.java:287)       在org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:198)       在org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:192)       在org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:226)       在org.codehaus.jackson.impl.Utf8Generator._flushBuffer(Utf8Generator.java:1754)       在org.codehaus.jackson.impl.Utf8Generator.flush(Utf8Generator.java:1088)       在org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1615)       在org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:559)       at org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:194)       在org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)       在org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)       在org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)       在org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290)       在org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203)       ... 78更多

很抱歉,如果我打印的堆栈跟踪太多......

我想知道如果我不打印堆栈跟踪,我怎么能让异常消失?谢谢:))

1 个答案:

答案 0 :(得分:4)

看起来您的异常由框架记录并抛出到您的客户端代码中。可能要么无法抑制它,要么(如果通过日志记录API记录),您可以使用适当的日志记录级别配置来控制它。