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("程序顺利进行中");
}
}
}