try catch finally的应用

来源:互联网 发布:深层网络进入方法 编辑:程序博客网 时间:2024/04/29 16:07

 

在使用try catch finally时,有时会出现upexcepted exception ... 这里就是你的try块写得有问题了。可能是由于需要

try catch的地方,你没有使用。下面的一个是正常的。

    private void QuearyDB(String strSQL)
    
{
        Connection con 
= db.getConn ();
        Statement stmt 
= null;
        ResultSet rs 
= null;
        
try
        
{
            stmt 
= con.createStatement();

            String querySql 
= strSQL;

            
/** 执行查询操作*/
            rs 
= stmt.executeQuery(querySql);

            
while(rs.next ())
            
{
                
/** 关键词 */
                String keyWord 
= rs.getString (KEY_WORD);
                
/** 出现次数 */
                
int    count   = rs.getInt (APPEAR_COUNT);
                
                
double possibleOne = (double)count/ (double)GoodAppearCountSum;

                
/** 获取另外一个表的相关数据 */
                
double possibleTwo =GetAnotherPossible(keyWord);
                
                
double probility = possibleOne/(possibleOne+possibleTwo);
                
                InsertPossible(keyWord,probility);

            }


        }

        
catch (SQLException e) 
        
{
            e.getStackTrace ();
        }
              
        
finally
        
{
            
if(rs!=null)
            
{
                
try
                
{
                    rs.close ();
                }

                
catch(Exception e)
                
{
                    e.getStackTrace ();
                }

                
            }

                        
            
if(stmt!=null)
            
{
                
try
                
{
                    stmt.close ();
                }

                
catch(Exception e)
                
{
                    e.getStackTrace ();
                }

                
            }

            
        }

    }
 
原创粉丝点击