aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception handling question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception handling question" Watch "exception handling question" New topic
Author

exception handling question

pradeepbill arumalla
Greenhorn

Joined: Jul 15, 2003
Posts: 19
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 ]

pradeep
shengzhong zhang
Greenhorn

Joined: Jul 15, 2003
Posts: 1
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

Joined: Jul 15, 2003
Posts: 19
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

Joined: Jun 23, 2003
Posts: 120
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 ]

Miki<br /> <br />SCJP 1.4, SCBCD 1.3
pradeepbill arumalla
Greenhorn

Joined: Jul 15, 2003
Posts: 19
thanks miki ...ur reasoning helped me understand the flow control.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception handling question