Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

exception handling question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

when i ran this program the error was .......
except.java:22:unreachable statement
System.out.println("print 7";
1 error
>execution finished
why is this error? when i take of the return statement at 1,it runs perfectly printing ...
output rint 4
print 6
so,folks please help me solve this riddle...
thanks
Edited By Corey McGlone: Added CODE Tags
[ July 16, 2003: Message edited by: Corey McGlone ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get ride of all the keyword"return".
then it's ok.
modify:

Edited By Corey McGlone: Added CODE Tags
[ July 16, 2003: Message edited by: Corey McGlone ]
 
pradeepbill arumalla
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u explain why is it correct after removing the return statement(statements)?
can u make this concept clear to me...shenzhong zhang.
thanks
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pradeepbill, what happens I think is the following:
1.the compiler determines that an exception is thrown in the try block (for sure!)
2.the exception thrown in the try block is correctly caught by one of the catch statements
3 then since all the catch blocks have a return statement, it means that the code after the finally block can never execute.
If for example you try to replace the try body with:
if (true) throw new RuntimeException();
you wills e that the code compiles ok! Since the compiler can NOT determine anymore that an exception is always thrown at runtime.
Anb BTW this has nothing really to do with exceptions, but program flow. This is very similar with:

where you get again a compiler error since "1" is never reachable.

You can read more in JLS 14.20 (unreachable statements)
Hope it helps
Miki
Edited By Corey McGlone: Added CODE Tags
[ July 16, 2003: Message edited by: Corey McGlone ]
 
pradeepbill arumalla
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks miki ...ur reasoning helped me understand the flow control.
 
Message for you sir! I think it is a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic