Home > Java > javaTutorial > body text

Code examples about the finally keyword in Java

Y2J
Release: 2017-05-03 14:43:13
Original
1516 people have browsed it

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!