aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return statement effect in Exception class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return statement effect in Exception class" Watch "return statement effect in Exception class" New topic
Author

return statement effect in Exception class

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi,

I have given the questions in the comments at Line # 10 and 11. Please advice


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Q1- Why would it? The finally block has nothing to do with this question, by the way.
Q2- It's telling you exactly why. If you return from both a try and catch block, how will you ever reach the code after the try/catch?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Kevin Workman wrote:Q1- Why would it? The finally block has nothing to do with this question, by the way.
Q2- It's telling you exactly why. If you return from both a try and catch block, how will you ever reach the code after the try/catch?


Hi Kevin,I changed the code and trying to understand the concept behind for these 2 points. Please advice
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
My answers do not change.

If you can't do it "by hand", then you should step through your program with a debugger to understand what it's doing.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4242
    
    7

In the following case:
Would you expect "Hello World" to be printed? No - the method returns before you get there. It's the same in the example you've given. return exits the method, not the block it's in.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Firstly, the code won't compile. You can't return a value from a void method.

Secondly, dude if you put a return in a try and a catch, how will you ever reach "After finally"?

And, I still don't understand why "After finally" is not being printed. It should, because once the exception is caught and dealt with, shouldn't it move on to the next line of code?
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Mathew Brown, the compiler doesn't know that i is actually < 20. But the compiler knows in the previous example that the code below won't ever be reached.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3387
    
    9
Rohit Ramachandran wrote:And, I still don't understand why "After finally" is not being printed. It should, because once the exception is caught and dealt with, shouldn't it move on to the next line of code?

Yes, the exception is caught and dealt with, but note the return statement in the catch block. Meaning that after the exception is dealt with (and the finally block is executed), the method returns.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Hmm thank you.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Mat, Let me make it simple. Why line 10 is giving an error as given below
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Dude, at line 3, the code can either reach the try part and then the catch part of the try/catch block. In both cases, you've put a return so there's no way you'll ever get to line 10. THe error says clearly, "Unreachable code"
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Even though I have only one return in catch block, Line#10 is still unreachable..Why..
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3387
    
    9
The compiler pretty much strips your catch block, and inlines it, because it sees that you will always throw an exception. It pretty much does this:So it sees that your last line will *never* be run.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Because you have a "throw" in the try part of the try/catch block, which means each time the try block is entered, it ends up in the catch block. The compiler knows this.

Just modify the code to this-



Hello World is still unreachable but you won't get the unreachable code error since the compiler doesn't know that x=5/0 will throw an ArithmeticException: divide by zero.

Do you understand it now?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Great !! Thanks Stephan & Rohit. I understand it now..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return statement effect in Exception class
 
Similar Threads
finally block with return statement
Exception Q
A runtime exception is throws in the catch clause:
explain the output
unreachable code in 'finally'