Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic