• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is finally block absolutely necessary?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic