应该怎么用finally释放占用的资源?
finally是在try-catch之后执行的,如果return在try-catch中,finally会在return后再执行,所以直接把释放资源的语句放在finally语句块中即可,例如常见的数据库连接
try{ Connection con = DriverManager.getConnection(url,username,pwd); Statement state = con.createStatement(); }catch(IOException e){ e.printStackTrace(); }finally{ con.close(); state.close(); }