谨慎使用java的try catch
来源:程序员人生 发布时间:2015-04-07 08:14:47 阅读次数:3322次
看看下面代码有何问题:
public static String writeFileToRemote(File file) throws Exception {
PostMethod filePost = new PostMethod(Common.getInstance().getProperty(
"project/remoteFileServer")
+ "/attach?action=upload");
try {
Part[] parts = new Part[4];
parts[0] = new StringPart("FILE_KIND", "2");
parts[1] = new StringPart("RECORD", "true");
parts[2] = new StringPart("FILE_KIND", "2");
parts[3] = new FilePart("FILE_PATH", file.getName(), file);
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
// 由于要上传的文件可能比较大,因此在此设置最大的连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
int status = client.executeMethod(filePost);
String file_path = "";
if (status == HttpStatus.SC_OK) {
// 从
服务器响应的串中获得fileId
String res = filePost.getResponseBodyAsString();
String retFileStr = "retobj.fileId = "";
res = res.substring(res.indexOf(retFileStr)
+ retFileStr.length());
file_path = res.substring(0, res.indexOf('"'))
.replace('|', '/');
}
file.delete();
return file_path;
} catch (Exception e) {
e.printStackTrace();
throw new Exception("写远程文件时出现异常!");
}
}
报错:
Caused by: java.lang.Exception: 写远程文件时出现异常!
com.age.sale.view.tool.ImportTool.writeFileToRemote(ImportTool.java:112)
此时根本没法定位毛病!
因此,应当把try catch去掉,如果要catch,必须把报错信息记录下来!并显示到前台!由于查询日志是很麻烦的1件事情。
本文出自:ouyida3的csdn blog
2015.3.5
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠