wood burning stoves*
The moose likes Features new in Java 7 and the fly likes try-with-resources from Java tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "try-with-resources from Java tutorial" Watch "try-with-resources from Java tutorial" New topic
Author

try-with-resources from Java tutorial

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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


When The Going Gets Tougher,The Tougher gets Going
Shiv Swaminathan
Ranch Hand

Joined: Jul 21, 2010
Posts: 48

Hi Deepak

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

Here is another working code:



SCEA EE5, CSM, PMP, IBM OOAD with UML, SCDJWS, SCWCD, SCJP 1.5
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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

Joined: Jul 21, 2010
Posts: 48

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try-with-resources from Java tutorial
 
Similar Threads
IOException
Your favorite new Java 7 feature
When the Statment After Finally clause will get Executed
Exam Objectives Flow Control.
TextFileIn