This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception handling" Watch "Exception handling" New topic
Author

Exception handling

Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
Hi there,

I have a situation where I have a for loop and I'm processing some files from a list in it. I need to continue processing files even if there is a exception. How can I do that. Is there a better way to do that

Thanks

- Harjit
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

Originally posted by Harjit Singh:
Hi there,

I have a situation where I have a for loop and I'm processing some files from a list in it. I need to continue processing files even if there is a exception. How can I do that. Is there a better way to do that

Thanks

- Harjit



Do not know if I got you right but you can do this...





Is this what you were looking for...?
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

... I agree, but want to add that you may also want a "finally" block. For example, you probably want to always try to close() the File object.

Kaydell
Harjit Singh
Ranch Hand

Joined: Feb 01, 2007
Posts: 77
Isn't that a bad practice to have a try {} inside a for loop or any of the loops ?

- Harjit
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Harjit,

Generally speaking, no, it isn't a bad practice at all. If you want to log the error in accessing or processing a single file then move on to the next one, this is the right way to do it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling
 
Similar Threads
Exception
Best way to process 500MB ASCII file
can tomcat process other files then jsp?
out of memory
Catch a root cause exception under a Java IO Exception