当前位置:主页 > 软件编程 > JAVA代码 >

Java关键字finally_动力节点Java学院整理

时间:2021-04-27 09:07:41 | 栏目:JAVA代码 | 点击:

1.如果执行了try块没有异常,则继续运行finally块中的语句,即使try块通过return,break,或者continue于最后的语句退出,finally中的语句也是会执行的。

总结:

不管是否出现异常,finally子句总是在块完成之前执行。

代码:

import java.util.Scanner;
public class DivideByTwo{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int x;
System.out.println("please enter integer:");
try{
String oneline=in.nextLine();
x=Integer.ParseInt(oneLine);
System.out.println("half of x is"+(x/2));
}
catch(NumberFormatException e){
System.out.println(e);
} 
finally{
System.out.println("hello world!");
}
}
} 

您可能感兴趣的文章:

相关文章