What is the proper way to handle a NumberFormatException when it is expected?

I'm running into this situation where I need to parse a `String` into an `int` and I don't know what to do with the `NumberFormatException`. The compiler doesn't complain when I don't catch it, but I just want to make sure that I'm handling this situation properly. private int getCurrentPieceAsInt() { int i = 0; try { i = Integer.parseInt(this.getCurrentPiece()); } catch (NumberFormatException e) { i = 0; } return i; } I want to just simplify my code like this. The compiler doesn't have a problem with it, but the thread dies on the `NumberFormatException`. private int getCurrentPieceAsInt() { int i = 0; i = Integer.parseInt(this.getCurrentPiece()); return i; } Google CodePro wants me to log the exception in some way, and I agree that this is best practice. private int getCurrentPieceAsInt() { int i = 0; try { i = Integer.parseInt(this.getCurrentPiece()); } catch (NumberFormatException e) { i = 0; e.printStackTrace(); } return i; } I want this method to return `0` when the current piece is not a number or cannot be parsed. When I don't catch the `NumberFormatException` explicitly, does it not assign the variable `i`? Or is there some default value that `Integer.parseInt()` returns? General style says that if I catch an exception, I should log it somewhere. I don't want to log it. It's normal operation for this exception to be thrown sometimes, which also doesn't sit well with me. I cannot find a function, however, which will tell me if `Integer.parseInt()` will throw an exception. So my only course of action seems to be to just call it and catch the exception. The [javadoc][1] for `parseInt` doesn't help much. Here are the specific questions I'd like to know: * Is there a method that I can call that will tell me if `Integer.parseInt()` will throw a `NumberFormatException` before calling it? Then I would have no problem logging this, since it should never happen. * If I simply do not catch the exception, will the valiable not get assigned? Then I will simply initialize it to the value that I want when it's not a number and not catch the exception. * Is there a way to mark the exception somehow explicitly that I don't care about it? I'm thinking this would be something similar to `AWTEvent.consume()`. If so, then I will do this so that Google CodePro doesn't see this as "unlogged". [1]: http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
"If I simply do not catch the exception, will the valiable not get assigned? Then I will simply not catch the exception." - if you're unsure about whether this is a viable option, I suggest you try it (and walkthrough with a debugger) to be 100% sure you understand what happens in this case. I don't mean to sound like I'm talking down, but I feel that having a solid understanding of exceptions is important.

以上就是What is the proper way to handle a NumberFormatException when it is expected?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏