This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I am a C++ developer and new to Java. When I read about finally, I am wondering why this block is necessary. As in C++ the control will go to the statement after catch block if it encounters exception or after normal try block. It looks to me finally is not needed. To be more clear, look at the section follows, try{ ... } catch(){ ... } end: ... What I mean is the control flow will always go to statement after label end under both normal and exception circumstances. Can anyone give me some advices?
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
Rourou The simplest example of a use for finally would be if you are doing some I/O work. say you open a file and in the try block have some sort of exception thrown. If you have multiple catch blocks instead of trying to close the file in each of them you could just do it once in the finally block. someone else can probably give a better example than this hope it helps
------------------ Dave Sun Certified Programmer for the Java� 2 Platform
Dave
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
It won't always go there. If you have an uncaught exception, it could end. Or another instance, you could code in the catch block to return or execute some other routine that may not come back. If you have a throws on your method and that exception is within your try but not caught it won't go there. There may be other instances as well. So no it doesn't always flow through.
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
In my opinion, the <code>finally</code> clause is one of the very biggest improvements, going from C++ to Java. As a previous respondent has said, if you catch an exception, your catch-handler code may not allow execution to proceed to the next block; it may throw or return, for instance. But even more important, I feel, is the case of exceptions which you do not catch within the current method. Finally allows you to ensure that clean-up action is taken, no matter what happens, even if there is an exception that you did not handle, or perhaps you did not even know was possible. In C++, because there is no finally, you have to find out all possible exceptions that can happen and ensure that you catch them all and do appropriate clean-up. This is tedious and prone to mistakes. People often under-value exception handling code. In my experience, this is a good way to differentiate between good and excellent code. The good code probably works right in the normal situation, but the excellent code works right in the exceptional conditions, too.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
Rourou For a more technical discussion of how a try-catch-finally blaock is handles you can check out the JLS section 14.19.2. this doesn't go into uses for them just how they work. About the only time the finally block wouldn't execute would be if some sort of systme error occurs or the JVM itself shutdown - maybe a System.exit() would do it. A good place to see what they can be used for would be the Java tutorial from Sun. hope that helps
------------------ Dave Sun Certified Programmer for the Java� 2 Platform