Use Java's try catch with caution

Source: Internet
Author: User
Tags try catch

See what's wrong with the following code:

public static String writefiletoremote (file file) throws Exception {Postmethod filepost = new Postmethod (Common.getinstan CE (). GetProperty ("project/remotefileserver") + "/attach?action=upload"); try {part[] parts = new Part[4];p arts[0] = new Stringpart ("File_kind", "2");p arts[1] = new Stringpart ("RECORD", "true");p arts[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 () or//Because the file to be uploaded may be larger, so the maximum connection timeout is set Client.gethttpconnectionmanager (). Getparams (). setconnectiontimeout, int status = Client.executemethod (FilePost); String file_path = "", if (status = = HTTPSTATUS.SC_OK) {//gets fileidstring res = filepost.getresponsebodyasstring from the server response string ( ); 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 ("an exception occurred while writing the remote file! ");}}
Error:

Caused by:java.lang.Exception: An exception occurred while writing remote files! Com.age.sale.view.tool.ImportTool.writeFileToRemote (importtool.java:112)
There is no way to locate the error at this time!

Therefore, try catch should be removed, if you want to catch, you must record the error message! And show to the front desk! Because querying the log is a very troublesome thing.

This article originates from: Ouyida3 's CSDN Blog

2015.3.5

Use Java's try catch with caution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.