• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception handling question

 
pradeepbill arumalla
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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 ]
 
shengzhong zhang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Miki Muzsi
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks miki ...ur reasoning helped me understand the flow control.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic