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