Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to skip exception in a while loop

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Campbell Ritchie
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 142
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic