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
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...
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.
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 ]
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 ]
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.
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 ]
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 ]
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 ]
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