ConnectException异常后面return不走了,catch中printStackTrace也不

ks_ly 2019-09-20 03:40:37
try {
URL url = new URL("http://"+getIp()+":"+getPort()); //url地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/json");
connection.connect();
os = connection.getOutputStream();
os.write(upload.getBytes("UTF-8"));
reader = new BufferedReader( new InputStreamReader(connection.getInputStream()));
String lines;
sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "UTF-8");
sbf.append(lines);
}
logger.info("下传报文为: "+sbf);
responseLine = getResult(sbf.toString());
connection.disconnect();
}catch (MalformedURLException e) {
responseLine ="创建URL出错!";
logger.error(responseLine);
e.printStackTrace();
}catch(ConnectException e){
responseLine = "创建链接失败!";
logger.error(responseLine);
e.printStackTrace();
}catch (IOException e) {
responseLine = "发送报文错误!";
logger.error(responseLine);
e.printStackTrace();
}catch (JDOMException e ){
responseLine = "解析下传报文错误!";
logger.error(responseLine);
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally {
try{
os.close();
reader.close();
sbf.setLength(0);
}catch (IOException e) {
e.printStackTrace();
}
}
logger.info("cmd HttpDatagram result."+responseLine);
return responseLine;
...全文
228 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ks_ly 2019-09-23
  • 打赏
  • 举报
回复
改了一下finally 里的内容,responseLine可以返回了。但是 e.printStackTrace();还是没执行,log.error执行了。这些问题是怎么回事?? finally { try{ if (os != null) { os.close(); } if(reader !=null){ reader.close(); } sbf.setLength(0); }catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } }
ks_ly 2019-09-23
  • 打赏
  • 举报
回复
没有源码debug不了!我也是写完放在服务上运行的,responseLine我在方法最开始初始化了。
瘦死的黑骆驼 2019-09-20
  • 打赏
  • 举报
回复
不会debug吗,debug跑一下很简单啊,目测可能是你logger.error(responseLine);这个Logger没初始化报了空指针吧,空指针你没有catch
ks_ly 2019-09-20
  • 打赏
  • 举报
回复
ConnectException异常后面return不走了,catch中printStackTrace也不执行,请问是什么情况。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧