java关键字finally不管是否出现异常,finally子句总是在块完成之前执行。下面通过实现代码给大家介绍Java关键字finally相关知识,需要的的朋友参考下吧
1.如果执行了try块没有异常,则继续运行finally块中的语句,即使try块通过return,break,或者continue于最后的语句退出,finally中的语句也是会执行的。
总结:
不管是否出现异常,finally子句总是在块完成之前执行。
代码:
import java.util.Scanner;public class pideByTwo{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!");}}}
以上就是关于Java中关键字finally的代码示例的详细内容,更多请关注其它相关文章!
家!甜蜜的家!天下最美好的莫过於家