Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么try块中引发的异常后的代码未执行?如果未处理异常,则控件将退出?_Java_Exception_Try Catch_Try Catch Finally - Fatal编程技术网

Java 为什么try块中引发的异常后的代码未执行?如果未处理异常,则控件将退出?

Java 为什么try块中引发的异常后的代码未执行?如果未处理异常,则控件将退出?,java,exception,try-catch,try-catch-finally,Java,Exception,Try Catch,Try Catch Finally,如果您提取方法并添加额外的try-catch块,我认为您可以理解该行为,如下所示: class TestFinallyBlock1{ public static void main(String args[]){ try{ int data=25/0; System.out.println(data); } catch(NullPointerException e){S

如果您提取方法并添加额外的try-catch块,我认为您可以理解该行为,如下所示:

class TestFinallyBlock1{  
    public static void main(String args[]){  
        try{  
            int data=25/0;  
            System.out.println(data);  
        }  
        catch(NullPointerException e){System.out.println(e);}  
        finally{System.out.println("finally block is always executed");}  
        System.out.println("rest of the code...");  
    }  
} 

有关更多详细信息,请参阅

Output:finally block始终执行线程主java.lang.ArtihmeticException:/by Zero无论是否处理异常,try block中代码的异常行之后的代码始终不会执行,为什么会这样??
public class TestFinallyBlock1 {
    public static void main(String args[]) {
        try {
            throwArithmeticException();
            System.out.println("rest of the code...");
        } catch (ArithmeticException e) {
            System.out.println(e);
        }
    }

    private static void throwArithmeticException() {
        try {
            int data = 25 / 0;
            System.out.println(data);
        } catch (NullPointerException e) {
            System.out.println(e);
        } finally {
            System.out.println("finally block is always executed");
        }
    }
}