您的位置:首页 > 编程语言 > Java开发

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

2017-10-11 16:12 741 查看
 
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!");

}

}



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息