Common handling of Java exception handling

Source: Internet
Author: User

One of the counter examples: discarding exceptions
Conclusion: Since the exception is caught, it needs to be dealt with appropriately. Do not catch the exception and discard it, ignoring it.


Inverse example Two: Do not specify a specific exception
Conclusion Two: Specify the specific exception type as much as possible in the catch statement, and use multiple catch if necessary. Do not attempt to handle any exceptions that may occur.


Third of the counter example: Occupy Resources not released
Conclusion Three: Ensure that all resources are properly released. Make full use of the Finally keyword.


Inverse example Four: does not describe the exception details
Conclusion Four: In the exception processing module to provide an appropriate amount of error reason information, organize error information to make it easy to understand and read.


Counter Example V: too large try block
Conclusion Five: Try to minimize the volume of the try block.


Inverse Example VI: output data is incomplete
Conclusion Six: Comprehensively consider the possible anomalies and the impact of these anomalies on the execution process.

OutputStreamWriter out =... java.sql.Connection Conn= ...Try{Statement stat=conn.createstatement (); ResultSet RS=Stat.executequery ("Select UID, name from user");  while(Rs.next ()) {out.println ("ID:" + rs.getstring ("UID") + ", Name:" + rs.getstring ("name"))); }}Catch(SQLException sqlex) {out.println ("Warning: Data not complete"); Throw NewApplicationException ("SQL Error reading data", Sqlex);}Catch(IOException ioex) {Throw NewApplicationException ("IO Error writing data", Ioex);}finally{if(Conn! =NULL) {Try{conn.close (); }Catch(SQLException sqlex2) {System.err ( This. GetClass (). GetName () + ". MyMethod-Cannot close database connection:" +sqlex2.tostring ()); }}if(Out! =NULL) {Try{out.close (); }Catch(IOException ioex2) {System.err ( This. GetClass (). GetName () + ". MyMethod-Cannot close output file" +ioex2.tostring ()); }}}

Common handling of Java exception handling

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.