wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finally" Watch "finally" New topic
Author

finally

Ruediger Bachmann
Greenhorn

Joined: Jun 27, 2002
Posts: 6
Hello,
I have a question regarding the finally clause (even if it might not be the generally accepted way of programming).
Why do I not have to catch the FileNotFoundExceptions in the following coding (it's because of the return statement, I already got to know this but why does the compiler not complain about this)?

Can somebody help me on this one?
Thanks and kind regards,
Ruediger


SCJP 1.4
Eddie Long
Ranch Hand

Joined: Nov 02, 2003
Posts: 69
I would say the reason for the compiler not complaining is due to the fact that Java says you should Catch or Specify an exception.
The fact that you are using a try block means that you are potentially catching the exception and doing what you think is the appropriate thing i.e. executing the finally block!
This is totally plausible if there is nothing useful to be done in the exception handler.
I hope this makes sense...


...and so help me God.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Ruediger,
1. Are you familiar with the Web site called the Java Spec Report � a collection of unofficial errata for the JLS.
Here is a page that discusses your example:
http://www.ergnosis.com/java-spec-report/java-language/jls-14.17-b.html
----
2. An intuitive way to think about this is to consider what happens at run-time. This is described in JLS 14.19.2
Briefly, the try block completes abruptly and the REASON is a throw of an instance of FileNotFoundException. Then the finally block is executed. The finally block completes abruptly and the REASON is a return. The original REASON is discarded and forgotten.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I suggest that you keep a folder of examples of �tricky try statements�. This is how I would save your example.

You might also save some notes, including the link to this post or
the intuitive reason, the JLS references 14.17 and 14.19.2 and
http://www.ergnosis.com/java-spec-report/java-language/jls-14.17-b.html
[ November 03, 2003: Message edited by: Marlene Miller ]
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Hi Marlene
Congratulations on becoming an almost 100% SCJP 1.4 (we know that even that score doesn't do justice to your knowledge).
I have a general question about your suggestion above. Maybe you or the moderators of this site could answer that. If I save a thread (post, not Java thread) as a bookmark and try to access it 6 months down the line, what are the chances that it would still be accessible?
I hope you understand where I'm coming from ... do I need to copy-paste the contents of the posts I want to save or can I just bookmark the URL without worrying about it being moved around or taken off frequently.
Thanks
Harwinder
[ November 04, 2003: Message edited by: Harwinder Bhatia ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Harwinder, when I do a search on this forum, I see that the posts go back as far as 1999. I hope our moderators can reassure us.
Thank you so much for your kind congrats. You ask so many good questions. I am sure you are learning just as much.
This example is surprising, don't you think? although it makes sense when I think about it. I'd say it's the trickiest try statement I've seen.
[ November 04, 2003: Message edited by: Marlene Miller ]
Ruediger Bachmann
Greenhorn

Joined: Jun 27, 2002
Posts: 6
Hi Marlene,
thank you for the detailed answer and the link which was very helpfull for the understanding of that problem.
Kind Regards,
Ruediger
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thanks Ruediger. I was worried you might not see my response, because it was so long ago (2 days) that you posted your question. Now I don't have to worry. I glad the Java Spec Report made sense to you.
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi Ruediger,
Wow..great question. Could you tell us the source of this question? Or is this something you implemented while studying?
Thanks.
[ November 04, 2003: Message edited by: Cathy Song ]
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Here's another variation of the above code:

This program doesn't compile.
Maybe the reason is obvious to everybody but wasn't to me. I thought that having the return statement in the 'try' block was sufficient but ...
Cheers
Harwinder
[ November 05, 2003: Message edited by: Harwinder Bhatia ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
The compiler error I get is: missing return statement
Hi Harwinder, the way I try to understand why compiler errors occur (or don't occur) in try statements is to think about what could happen at run-time.
In your example, if the FileInputStream constructor throws an exception, the exception is handled and the try statement completes normally. Then the code drops of the end of the method. There is no instance of Object to return.
In JLS jargon, a try statement completes normally OR it completes abruptly with a Reason (throw, return, break, continue).
In the first example, if the FileInputStream constructor throws an exception, the try block completes abruptly and the Reason is a throw. The finally block is executed and the try statement completes abruptly and the Reason becomes a return instead of a throw. An instance of Object is always created and returned.
[ November 05, 2003: Message edited by: Marlene Miller ]
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Hi Marlene
I get the same compiler error. The problem as well as the cause became clear after I saw the error message. What I was trying to say above was that if I had encountered this problem in the exam for the first time, I may have tripped. Ofcourse, now it makes "obvious" sense.
I have been practising writing, compiling and running code and the more I practice the stronger I feel about its importance.
Cheers
Harwinder
[ November 06, 2003: Message edited by: Harwinder Bhatia ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Harwinder, I reread your example. I see now that you were not asking a question. You were showing us a related example. Something to help us.
Ruediger Bachmann
Greenhorn

Joined: Jun 27, 2002
Posts: 6
Hi Cathy,
I apologize for the delay of my answer but I have been involved in serveral other software development issues during the last days.
I ran against the problem when I was studying the statement 'catch or specifiy' a checked exception and that finally can only be prevented from being completed by an exception inside finally, by System.exit, by switching off the computer or by killing the corresponding process but there was no real requirement behind this (apart from my intention to pass the SCJP exam sometime in the future).
Kind Regards,
Ruediger
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Thanks Ruediger.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finally
 
Similar Threads
IOException while reading objects
Exception within method with return value
order of output
static methods
what is the concept behind?