• 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

Can we write a try catch block inside a finally block

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we write a try catch block inside a finally block and in any cases we write the same inside a finally block.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek, welcome to the Ranch!

About your question: Why don't you just try it out? Experimenting is the best way to learn things.
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, you can nest them, however it can get a little confusing if you have too much of that going on, from my experience
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I add to Vivek's question? This is something I've been wondering about too. For some of the IO classes, the close() method can throw an exception. However, putting another try/catch block inside the finally block, or nesting try/catch/finally blocks in some other way, seems awkward. What is the preferred way of handling this situation?
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kurt Van Etten wrote:Can I add to Vivek's question? This is something I've been wondering about too. For some of the IO classes, the close() method can throw an exception. However, putting another try/catch block inside the finally block, or nesting try/catch/finally blocks in some other way, seems awkward. What is the preferred way of handling this situation?



I prefer to have only one source of try/catch in each method. Then I logically order them so that they either can take care of the Exception or throw it to the calling method to handle.

A typical such construction is when handling files. I have a method that handles the opening of the file and returns the File-instance if it is opened. If I get a File (sometimes I check for not null and sometimes I use an Exception, depends on that assignment) then I start the reading process. If the result from the reading is OK, then I go ahead and close the file. It might be a bit overkill and I have had quite some discussions about "duplicate code, since they throw the same exception", but it helps in the debugging when several calls in a call chain can throw the same exception.

Not a clear answer to your question...
 
Vivek Hingorani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even i answered the same in an interview that i guess we can write but not sure of the use..But thinking logically any code in a finally block should not throw a exception and we should not complicate things....
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read about this new feature related to this discussion in JDK 7..

http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic