• xorium (unregistered)

    It's a pity he did not fix the atrocious curly brace style while he was at it. This is java unworthy.

  • Rob (unregistered)

    Does Java not have some form of the TryParseInt function?

  • Richard Brantley (unregistered)

    This is the kind of method I re-read multiple times just to try to back into the thought process that produced it.

  • (nodebb)

    "only to find 39 copies of this function, with minor variations." If only there were a way to reuse code in other parts of the project...

  • (nodebb) in reply to Rob

    No, Java doesn't have a tryParse built-in. There's just the parseInt used here that throws an exception (that Java forces a catch block for): https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Integer.html#parseInt(java.lang.String)

    But it's a common thing to exist in utility libraries that many projects would be including anyway, like Apache Commons Lang: https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/math/NumberUtils.html#toInt-java.lang.String-

    Of course, it might be implemented by just catching the exception of the built-in parseInt, so you're playing the Java exception performance penalty anyway for cases where it isn't a number: https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/math/NumberUtils.java#L1528

  • pedagog (unregistered)
    Comment held for moderation.
  • Rafael (unregistered)
    Comment held for moderation.
  • Duke of New York (unregistered)

    If trying you are, doing you are not.

Leave a comment on “A Trying Block”

Log In or post as a guest

Replying to comment #680471:

« Return to Article