Lara
Lara

Reputation: 167

Executing return statement after try block without finally block

I have a code in Android in a method:

try {  
    something();  
    return obj;  
}
catch (Exception e) {   
    e.printStackTrace();
}
return null;

When I run the app in debugging mode both return statements get executed, even there is no finally block and compiler does need second return(when I clean it compiler says "missing return statement") how this can be possible? Is this always the case for try blocks even without finally?

Upvotes: 0

Views: 231

Answers (3)

Al.Boldyrev
Al.Boldyrev

Reputation: 486

This is not possible. It's just shown by debugger that it's out of your try-catch block. Highlighted line does not means it's executed.

Upvotes: 1

NikNik
NikNik

Reputation: 2301

You need second return (return null;) because if "something()" throws exception, then the first return (return obj;) will not be executed

Upvotes: 2

Shivam Arora
Shivam Arora

Reputation: 364

Impossible, Share your full code. Every function executes only one return statement i.e. when the "return" statement in your function is encountered, The execution of the function is halted and a value whether user defined or primitive is returned or you can say outputted to the user.

Upvotes: 0

Related Questions