谨慎使用java的try catch

来源:互联网 发布:数据集成工程师 编辑:程序博客网 时间:2024/06/08 11:58

看看下面代码有何问题:

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) {// 从服务器响应的串中取得fileIdString 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,必须把报错信息记录下来!并显示到前台!因为查询日志是很麻烦的一件事情。

本文出自:ouyida3的csdn blog

2015.3.5

0 0