will this finally block execute?

Possible Duplicate:
In Java, does return trump finally?

I came across a java code snippet in a dao implementation .It returns a List as shown below.

After the 'return' statement is executed,the finally block tries to close the session.Will this work? or will the session remain open?

thanks

mark

import org.hibernate.SessionFactory;
import org.hibernate.Criteria;
...
public List<Item> findItems(String name) {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();
    try{
        Criteria cri = session.createCriteria(Item.class);
        return (List<Item>) cri.add(Restrictions.eq("name", name)).list();
    } finally {
        session.close();
    }
}

终止块不会被执行的唯一时间是如果你终止你的程序,如System.exit(0)或强制退出等。所以答案是:是的,你的会话将被关闭。


阅读finally块,是的,finally块将在return后执行。


Yes, finally blocks will always execute (*).

The finally block will execute "after" the return in the sense that the return value will be computed and remembered before the finally block is executed.

(*) caveat: unless something causes the JVM to end operation alltogether.

链接地址: http://www.djcxy.com/p/73710.html

上一篇: 在finally块中将null指定给变量

下一篇: 这将最终阻止执行?