aspose file tools*
The moose likes Beginning Java and the fly likes how to skip exception in a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to skip exception in a while loop" Watch "how to skip exception in a while loop" New topic
Author

how to skip exception in a while loop

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
code like

*************
public void test() throws Exception {

.... // have a variable "sToken"
while(sToken.hasMoreTokens()) {
handleToken(sToken.nextToken().toString())
}
}

public void handleToken(String s) {
try {
// do something using input "s"
} catch(Exception e) {
e.printStackTrace();
}
}
**************************

Question -- When I loop through "while", if the first token I pass encounters some exception in "handleToken" method, then I can not
handle the second one. What I like is if one token gets exception, just record it and move on to the next token.

How can I do that ?

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You mean that the handleToken() method may throw an Exception?

Never ignore an Exception, always do something with it, even if it is only e.printStackTrace();

And try to find out which Exception you are likely to suffer; it is usually a bad idea to declare throws Exception or catch (Exception). You are far better off with catch (MyParticularException mpe) . . .

If you put a try-catch inside your loop, won't that sort it all out for you?
mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
Originally posted by Campbell Ritchie:
You mean that the handleToken() method may throw an Exception?

Never ignore an Exception, always do something with it, even if it is only e.printStackTrace();

And try to find out which Exception you are likely to suffer; it is usually a bad idea to declare throws Exception or catch (Exception). You are far better off with catch (MyParticularException mpe) . . .

If you put a try-catch inside your loop, won't that sort it all out for you?



actually in my "handleToken()" method, I handled the exception (print the trace), I found if the first token has an exception, the loop stops there instead of first print the exception trace and then move to the second token, which I expected. Why ? I thought that, since the exception is handled, it should move to the next token, right ?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Campbell Ritchie:
Never ignore an Exception, always do something with it, even if it is only e.printStackTrace();


I see what you're trying to convey here - that exceptions aren't just an annoyance that must be squashed, but are something you must think about and handle.

However, it's not true that you should never ignore an exception; there are cases when it is the right thing to do. In those circumstances, I would advise including a comment in the otherwise-empty "catch" block, explaining why there is nothing to do in this case.



Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Well, I suppose I shall have to quote Campbell's Rules about Exceptions.
  • The number of different opinions obtained about exceptions is equal to the number of different people asked!
  • For every rule about Exceptions, no matter how hard-and-fast or engraved-in-stone-and-silicon, somebody can find a special case where it doesn't apply!
  • As for losing the next token, I don't know about that. Something is obviously happening if you get an Exception which loses you the next token, but we can't tell what.

    It's not an EndOfFileException, by any chance? That would explain it simply.
    mark I thomas
    Ranch Hand

    Joined: Apr 07, 2008
    Posts: 86
    I guess I didn't present my question clearly. I knew I should handle the exception. My question was --- Suppose I have 5 tokens, if the 2nd token causes exception, I want to log it or print out the trace, BUT continue for the rest tokens. What I found was -- no matter where I put the try/catch, it can't continue the the rest of tokens. And I can't figure out why the code isn't working the way I want it to.

    please let me know how I can handle and continue the loop.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3739
        
      16
    Your code should do what you want. As Campbell said, something else must be causing the remaining tokens to be lost when an exception is thrown.


    Joanne
    Guido Sautter
    Ranch Hand

    Joined: Dec 22, 2004
    Posts: 142
    What type of exception are you actually getting? If an exception is thrown while invoking handleToken(), not inside the actual method body, then this exception will not be cought by the try/catch that encloses the method body. For instance, if sToken.nextToken() returns null, sToken.nextToken().toString() will throw a NullPointerException. One of the downsides of inlining ...

    There's (at least) two ways of dealing with this:
    - put the loop body in a try/catch block
    - change handleToken() to take an Object argument and invoke toString() inside the method body
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to skip exception in a while loop