JAVA进阶2.4——finally

来源:互联网 发布:iphone硬件检测软件 编辑:程序博客网 时间:2024/06/11 15:55

finally语句是起到异常处理出口的作用,用在try...catch语句的最后,无论是否出现异常(catch中的语句是否被执行),Finally中的语句都会被执行,这样可以更好的控制程序的走向。

import java.util.*;class Test{public static void main(String[] args)throws Exception{Scanner sc=new Scanner(System.in);int a,b,c;try{a=sc.nextInt();b=sc.nextInt();c=a%b;System.out.println("余数为"+c);}catch(Exception e){System.out.println("输入有误");}finally{System.out.println("程序顺利进行中");}}}


0 0
原创粉丝点击