Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try-with-resources from Java tutorial

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiv Swaminathan wrote:
My requirement is ....is it possible to have a normal try{} block with catch(Exception e) in above code with try-with-resources feature of Java SE 7 feature.


Based on my understanding of the above statement, I think this is what you are looking for:

If you want to use the try with resources feature of Java 7 with the normal try-catch block prior to Java 7, then you must hand-code your finally block and also add proper exception handling catch while opening the resource.
The Java 7 try-with resources feature is essentially doing that for you.

The try-catch version for Mike's try-with-resources code posting



@Shiv Swaminathan,you have repeated the same thing which was done in earlier posts.
i want a working example of a combination of try with catch block with try-with-resources.both should be incorporated in a single working example.
hope my question is clear to you.you are explaining a concept prior to java SE 7.i don't see try-with-resource feature in your above code.
can you alter it to support try-with-resource feature in your above code.

i want both try with catch feature and try-with-resource feature to be used in a single Java example which should be working.


Regards,
Deepak Lal
 
Shiv Swaminathan
Ranch Hand
Posts: 48
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak

I understand your question now.
I think @Champbell has expanded your example.

Here is another working code:

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you upload lookup.txt file please so that it will be helpful for others to understand Shiv.
Appreciate your help in this regards.

--
Regards,
Deepak Lal
 
Shiv Swaminathan
Ranch Hand
Posts: 48
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lookup.txt is a text file which can be created easily.

Open a notepad. Type some words or sentences there.
Save the file in your C: with name as Lookup. And you will see that notepad will save it with extension .txt

I'm not able to attach the file here as .txt files are not allowed to be attached in message postings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic