问题: http://www.iteye.com/problems/77232
抛出异常:finally中的异常会先于try中的异常。
public static void test11() {
try {
System.out.println("try");
int i = 1 / 0;
} finally {
String s = null;
s.length();
System.out.println("finally");
}
System.out.println("end");
}
public static void test11_1() {
try {
test11();
} catch (Exception e) {
e.printStackTrace();
}
}
这段代码捕获到的异常是:
try
java.lang.NullPointerException
如果去掉String s部分,抛出的异常是:
try
finally
java.lang.ArithmeticException: / by zero
事务提交,trycatch写法:
http://wenku.baidu.com/view/17e2f56c1eb91a37f1115ca4.html第4页
Connection conn = null;
try{
conn = getConnection();
conn.setAutoCommit(false);
// do something
conn.commit();
}catch(Exception e){
conn.rollback();
// do sth
}finally{
try{
conn.close();
}catch(SQLException e){
// do sth.
}
//...
}