In Java, is the “finally” block guaranteed to be called (in the main method)?

前端 未结 10 2045
执念已碎
执念已碎 2020-12-06 09:17

I\'m a Java rookie and I was wondering, if I have the following typical Java code

public class MyApp {
  public static void main(String[] args) {
    try {
          


        
10条回答
  •  囚心锁ツ
    2020-12-06 10:10

    Chris Cameron is correct. But normally a finally-block gets executed. Null pointer dereferece does exist in Java:

    try {
        List x = null;
        x.get(1); //throws the unchecked NullPointerException
    } finally {
        //will be executed
    }
    
    
    

    The finally-Block gets executed.

    提交回复
    热议问题