Home >Java >javaTutorial >Code examples about the finally keyword in Java
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!