8

If execution doesn't cause exception then control goes to finally block. So is the return statement in try block is being ignored by JVM? . Or if exception occurs then control goes to catch block there also it ignored return statment and control go to finally block and return from finally

  public class Helper {
     public int showException(int a, int b){

           try{
           int c=a/b;
           return c;
           } catch(Exception e){
                return 0;
           } finally{
               return 3;
             }
     }
  }
Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29
sonal
  • 91
  • 1
  • 6
  • 3
    'return' in catch block is bad design. – Falaque Mar 25 '13 at 14:09
  • 1
    Finally is always run no matter what, try removing the finally (that is if it's not needed). – SpaceCowboy Mar 25 '13 at 14:10
  • @Falaque why is that? What if you need to return if your actions were succesfull? – Toon Casteele Mar 25 '13 at 14:11
  • 1
    @ToonCasteele if its successful, it would not go to catch block. – Falaque Mar 25 '13 at 14:15
  • then you always place the `return false;` at the end of your method for when it's unsuccesful? – Toon Casteele Mar 25 '13 at 14:17
  • @ToonCasteele not necessarily "return false" would be an exception condition. While control flow would come to catch only for a Exception condition. try...catch is designed to handle "exception". – Falaque Mar 25 '13 at 14:20
  • Just trying to figure out why it is bad design? What if you only return on a condition inside your try and return something else outside your try-catch block if that condition isn't met but your code gave no exceptions. And return something even different if you caught an exception? (I know you could rethrow the exception from there but I just wonder why it's bad for your code) – Toon Casteele Mar 25 '13 at 14:24
  • @ToonCasteele, this(http://stackoverflow.com/questions/77127/when-to-throw-an-exception) and this(http://programmers.stackexchange.com/questions/107723/arguments-for-or-against-using-try-catch-as-logical-operators) would give more idea. – Falaque Mar 25 '13 at 14:24
  • What i am asking is if there is no exception in try, then return in try should execute or if exception occurs, return in catch should executed and then control goes to finally. Does it mean that JVM ignoring the return in try or catch block – sonal Mar 25 '13 at 14:26
  • So it looks like it's more a thing of coding conventions and has no real down side otherwise, or am I wrong? Thanks for the info, I'll read your links through more thoroughly. EDIT: The second post tells me about to down side for your flow and speed. Thanks – Toon Casteele Mar 25 '13 at 14:26
  • returning from catch is not bad design per se. For instance if a method parses a String as an Integer using Integer.parseInt(..) then you might choose to return a default value from the catch block. – Adriaan Koster Mar 25 '13 at 16:28

5 Answers5

4

Because finally block will be executed every time whether you enter in try or in catch, I think that's why its called finally :)

FROM JAVA DOCS

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

Note: It won't be executed only when

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

Parth Soni
  • 11,158
  • 4
  • 32
  • 54
  • What i am asking is if there is no exception in try, then return in try should execute or if exception occurs, return in catch should executed and then control goes to finally. Does it mean that JVM ignoring the return in try or catch block – sonal Mar 25 '13 at 14:27
  • @sonal: Yes, it will ignore `return` of `try` only if you've defined `return` in `finally` – Parth Soni Mar 25 '13 at 14:32
  • @sonal: Code placed in a finally block must be executed whatever occurs within the try block – Parth Soni Mar 25 '13 at 14:33
2

By design, the return in the finally block does always take precedence.

Javier
  • 12,100
  • 5
  • 46
  • 57
0

finally block always executed whether the try catch block execute or not

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Read more here

tmwanik
  • 1,643
  • 14
  • 20
-1

Finally will get called last no matter if the catch block is called or not. Perhaps you want to read more documentation. MSDN Try Catch Finally

Mark Meyer
  • 3,643
  • 3
  • 23
  • 34