程序员人生 网站导航

谨慎使用java的try catch

栏目:php教程时间:2015-04-07 08:14:47

看看下面代码有何问题:

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

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐