The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java finally block after return statement

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java finally block after return statement Posted: Nov 15, 2017 10:44 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java finally block after return statement
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • finally block will be executed always even exception occurs.
  • If we explicitly call System.exit() method then only finally block will not be executed if exit() call is before finally block.
  • There are few more rare scenarios where finally block will not be executed.
  1. When JVM crashes
  2. When there is an infinite loop
  3. When we kill the process ex:kill -9 (on unix)
  4. Power failure, hardware error or system crash.
  5. And as we discussed System.exit().
  •  Other than these conditions finally block will be executed always.
  • finally block is meant to keep cleaning of resources. Placing code other than cleaning is a bad practice
  • What happens if we place finally block after return statement? or
  • Does finally block will execute after return statement??
  •  As per the above rules Yes finally will always execute except any interruption like System.exit()  or all above mentioned conditions

 Program #1 : Java example program which explains how finally block will be executed even after a return statement in a method.

  1. package com.instanceofjava.finallyblockreturn;
  2. /**
  3.  * By: www.instanceofjava.com
  4.  * program: does finally gets executed after return statement??
  5.  *
  6.  */
  7. public class FinallyBlockAfterReturn {
  8.  
  9.     public static int Calc(){
  10.         try {
  11.             
  12.             return 0;
  13.         } catch (Exception e) {
  14.             return 1;
  15.         }
  16.  
  17. finally{
  18.             
  19.  System.out.println("finally block will be executed even when we
  20. place after return statement");
  21.         }
  22.  }
  23.     
  24. public static void main(String[] args) {
  25.         
  26.         System.out.println(Calc());         
  27. }
  28.  
  29. }



  
Output:


  1. finally block will be executed even when we place after return statement
  2. 0


 Can we place return statement in finally??
  • Yes we can place return statement in finally and finally block return statement will be executed.
  • But it is a very bad practice to place return statement in finally block.
Program #2 : Java example program which explains finally block with return statement in a method.



finally block after return statement

Read: Java finally block after return statement

Topic: How to use compile and matcher methods of Pattern class | Java Regex Previous Topic   Next Topic Topic: Constructors or Static Factory Methods?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use