Finally block
- Eclipse: Oxygen
- Java: 1.8
The finally block is the block that always gets executed no matter the program throws an exception or not. But we have to follow some basic conditions that the catch blocks always is after the throw block.
This could be used to execute an important segment of code we always want the compiler to execute no matter what is the state of the exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class App { public static void main(String[] args) { int x = 2; try{ System.out.println("We don't know, what will be the outcome "+x/2); }catch(RuntimeException e){ System.out.println("Runtime exception"); }catch(Exception e){ System.out.println("Exception!!!"); } finally{ System.out.println("This will be get printed"); System.out.println(); } } } |
Output
We don’t know, what will be the outcome 1
This will be get printed
Contributed by Poonam Tomar