Power node Java College collation

  • 2020-07-21 07:43:12
  • OfStack

1. If the try block is executed without exception, continue running the statement in the finally block, even if the try block exits through return, break, or continue at the end of the statement.

Conclusion:

The finally clause always executes before the block completes, whether or not an exception occurs.

Code:


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

Related articles: