Two Laptop Bag*
The moose likes Java in General and the fly likes coninue after catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "coninue after catch block" Watch "coninue after catch block" New topic
Author

coninue after catch block

Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Hi,

is it possible to do something like this..

try {
for(i=0;i <10;i++) {
// if any exception comes between 1-10. don't break. just log the exception and continue till the loop counter is over.
}
}catch(Exception e){

log the exception.

}


please provide some help.


Thanks,
Neeraj.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

not really an advanced question, but...

No. the point of an exception is that it is something DRASTIC that PREVENTS you from continuing. What you want is a method in your loop that you call when <whatever> happens, possible with a "continue" statement.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Thanks for your inputs. n sorry for putting in advance section.

I need to find a work around for it

Tnx
Neeraj
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
I need to process 10 files at one go.

if something is wrong with the first or second file processing should continue. this is my user requirement.

Now as a developer I need to figure out a solution for it.. This is not a unreasonable requirement..

There must be some work around in java for it. if not a direct approach to it.

I am unable to think of it

any appraoch guidance will be a great help.

thanks,
Neeraj
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

What exception are you getting? couldn't you simply do this:


[ October 29, 2008: Message edited by: fred rosenberger ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Set up an array of files.
Call a method which processes one file, and may throw an Exception.
Catch the exception and handle as appropriate.
Iterate through the array.

See whether that would work
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Originally posted by myself:
Set up an array . . .


That looks quite similar to Fred's solution.
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Any exception should be handled and logged and control should continue till the counter.

I tried by throwing a NullPointerExceptin. but the control stops after catch block. and the next statement executes. (as per the rules. ]

But I need some work around to continue the processing.

If it's not possible then SUN should do something in the next version for it.

I hope you would also agree this is not a unreasonable business requirement. there must be some work around in java to do this which I am not able think

Thanks again
Neeraj
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Neeraj,

No offense, but the problem you're describing is really not a problem at all -- it's an extraordinarily simple issue, and the solution has already been described perfectly by fred and Campbell. If that's not working for you, I suspect you're doing it wrong. Please show us the code you tried (I can't say I understand what you mean about the NullPointerException) and we'll help you get it working.

Meanwhile, as this is really just everyday Java, I'm moving this to the Intermediate forum.


[Jess in Action][AskingGoodQuestions]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

I guess to make the point more clear...

You CANNOT - nor should you WANT - to have the exception handling work any different that it does. if Sun changed it, it could cause all kinds of problems in existing code, and would defeat the purpose of having exceptions.

Having said that, what you want to do CAN be done, if you re-arrange where you're try-catch block is. the 'try' part should wrap only the critical part that can't proceed if an exception is thrown. For the situation you have described, your for-loop CAN proceed if an exception is thrown, so it should not be IN the try-block.
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
Thanks for inputs. May be I am unable to put the words properly. I want to process all the files. But if an excetion comes the processing stops after the control goes to catch block.


[ October 30, 2008: Message edited by: Neeraj Vij ]
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
your for-loop CAN proceed if an exception is thrown, so it should not be IN the try-block.


How will I log the exception ? Request you to please provide some sample code.

Thanks
Neeraj.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by Neeraj Vij:


How will I log the exception ? Request you to please provide some sample code.

Thanks
Neeraj.


Fred has already shown you what to do in his second post. Put the try catch statement inside your for loop (or your while loop as it has now become).


Joanne
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
This was the actual code which I started with.

Thanks again. I will try it out again and let you know.

Thanks,
Neeraj.
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315
It's working for me now. Thanks everbody for their inputs. It was my mistake.

Thanks
Neeraj.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: coninue after catch block
 
Similar Threads
Capture javamail error to file
Generate pdf file and display in a browser
NumberFormatException from DateFormat.parse() ???
exception in finally
logic:iterate problem