Home  >  Article  >  Java  >  Code examples about the finally keyword in Java

Code examples about the finally keyword in Java

Y2J
Y2JOriginal
2017-05-03 14:43:131554browse

java keyword finally Regardless of whether an exception occurs, the finally clause is always executed before the block is completed. The following is an introduction to the Java keyword finally related knowledge through the implementation code. Friends who need it can refer to it

1. If there is no exception after executing the try block, continue to run the statements in the finally block, even if the try block passes Return, break, or continue exits after the last statement, and the statements in finally will also be executed.

Summary:

Regardless of whether an exception occurs, the finally clause is always executed before the block is completed.

Code:

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!");
}
}
}

The above is the detailed content of Code examples about the finally keyword in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn