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;
             }
     }
  }
11
  • 3
    'return' in catch block is bad design. Commented Mar 25, 2013 at 14:09
  • 1
    Finally is always run no matter what, try removing the finally (that is if it's not needed). Commented Mar 25, 2013 at 14:10
  • @Falaque why is that? What if you need to return if your actions were succesfull? Commented Mar 25, 2013 at 14:11
  • 1
    @ToonCasteele if its successful, it would not go to catch block. Commented Mar 25, 2013 at 14:15
  • then you always place the return false; at the end of your method for when it's unsuccesful? Commented Mar 25, 2013 at 14:17

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: Yes, it will ignore return of try only if you've defined return in finally
@sonal: Code placed in a finally block must be executed whatever occurs within the try block
2

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

Comments

0

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

1 Comment

what about return in try/catch block, does JVM only return from finally.
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

Comments

-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

2 Comments

this is about java, not javascript
The link goes to javascript when I click on it...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.