如下面:
private Connection getFrom(){
try {
try { } catch (Exception ex) {
ex.toString());
}
} catch (IOException er) {
er.printStackTrace();
}
return n;
}

解决方案 »

  1.   

    语法上嵌套没问题,只要try能找到对应的catch或者finally就可以了.
    不过貌似实际意义不大,也没见有人这么用.
      

  2.   

    private Connection getConnectionFromDatabase(){
    Connection trueConn = null;
    FileProperty sqlProperty = null;
    try {
    sqlProperty = new FileProperty("SQLconnection.properties");
    try {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName( sqlProperty.getProperty("db.driverName"));
    ds.setUsername(sqlProperty.getProperty("db.username"));
    ds.setPassword(sqlProperty.getProperty("db.password"));
    ds.setUrl(sqlProperty.getProperty("db.URL"));
    trueConn = ds.getConnection();
    } catch (Exception ex) {
    System.out.println("数据连接出错了:" + ex.toString());
    }
    } catch (IOException er) {
    er.printStackTrace();
    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
    上面的try 是否正确?
    sqlProperty.close();
    return trueConn;
    }
      

  3.   

    可以但没必要这么写
    Connection trueConn = null;
    FileProperty sqlProperty = null;
    try {
    sqlProperty = new FileProperty("SQLconnection.properties");
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(sqlProperty.getProperty("db.driverName"));
    ds.setUsername(sqlProperty.getProperty("db.username"));
    ds.setPassword(sqlProperty.getProperty("db.password"));
    ds.setUrl(sqlProperty.getProperty("db.URL"));
    trueConn = ds.getConnection();
    } catch (SQLException ex) {
    System.out.println("数据异常");
    } catch (IOException er) {
    er.printStackTrace();
    } catch (Exception er) {
    er.printStackTrace();
    }
      

  4.   

    没看到你下面两句话不能close,close了,你返回的connect就没有用了
      

  5.   

    private Connection getConnectionFromDatabase() {
    Connection trueConn = null;
    FileProperty sqlProperty = null;
    try {
    sqlProperty = new FileProperty("SQLconnection.properties");
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(sqlProperty.getProperty("db.driverName"));
    ds.setUsername(sqlProperty.getProperty("db.username"));
    ds.setPassword(sqlProperty.getProperty("db.password"));
    ds.setUrl(sqlProperty.getProperty("db.URL"));
    trueConn = ds.getConnection();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (Exception e) {
    System.out.println("数据连接出错了:" + e.toString());
    } finally {
    if (sqlProperty != null) {
    sqlProperty.close();
    }
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
    //上面的try 是否正确?
    //try 块语法上完全可以嵌套,有问题的地方在 sqlProperty.close();
    //可以把多个嵌套的 try 合并以简化程序逻辑
    //另外捕获到异常最好包装成你自己的异常或者直接把异常抛出去让调用者决定如何处理
    return trueConn;
    }
      

  6.   

    看错了,以为是conn.close这眼神