• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return statement effect in Exception class

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have given the questions in the comments at Line # 10 and 11. Please advice
 
Ranch Hand
Posts: 151
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm thank you.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mat, Let me make it simple. Why line 10 is giving an error as given below
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though I have only one return in catch block, Line#10 is still unreachable..Why..
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great !! Thanks Stephan & Rohit. I understand it now..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic