对Finally进行说明
package com.catches;
import java.util.Scanner;
//try--catch原理
/*
把可能出现异常的代码放入try代码块中,然后将异常封装为对象,被catch后面的()中的那个异常对象接收,
接收以后:执行catch后面的{}里面的代码,然后try-catch后面的代码,该怎么执行就怎么执行。
*/
public class Demo02 {
public static void main(String[] args) {
try{Scanner sc = new Scanner(System.in);
System.out.println("别怀疑,输入两个数:");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println("输入两个数的商是:"+num1/num2);
//这部分代码相当于终止虚拟机;当程序中有此代码;finally不执行
System.exit(0);//这里是特殊位置这里如果有异常会将异常封装为对象,则不会关闭虚拟机
}catch(Exception ex){
throw (ex);
}
//finally中的这部分代表无论如何都会执行
finally {
System.out.println("这部分执行:关闭数据流,IO流,socket");
}
}
}