在预期时处理NumberFormatException的正确方法是什么? [英] What is the proper way to handle a NumberFormatException when it is expected?

查看:267
本文介绍了在预期时处理NumberFormatException的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这种情况,我需要解析一个 String 到一个 int 中,不知道该如何处理 NumberFormatException 。编译器没有抱怨,当我没有抓住它,但我只是想确保我正确地处理这种情况。

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;
}

我想简化我的代码。编译器没有问题,但线程在 NumberFormatException 上死亡。

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要我以某种方式记录异常,我同意这是最佳实践。

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;
}

我想要这种方法返回 0 当当前片段不是数字或不能被解析时。当我没有明确地看到 NumberFormatException 时,是否没有分配变量 i ?或者有一些默认值, Integer.parseInt()返回?

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?

一般风格说如果我抓到例外,我应该在某个地方登录。我不想登录。有时会抛出这个异常的正常操作,这也不适合我。我找不到一个函数,但是如果 Integer.parseInt()将抛出一个异常。所以我唯一的行动方式似乎是只是调用它并捕获异常。

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.

parseInt / dev /没有帮助。

The javadoc for parseInt doesn't help much.

以下是我想知道的具体问题:

Here are the specific questions I'd like to know:


  • 有没有一个可以调用的方法告诉我,如果 Integer.parseInt()将抛出一个 NumberFormatException 在调用它之前?那么我记录下来就没有问题,因为它永远不会发生。

  • 如果我根本没有抓到这个异常,那么值得注意的是不会被赋值?然后,我将简单地将其初始化为我想要的值,当它不是一个数字,而不是捕获异常。

  • 有没有办法明确地标记异常,我不在乎关于它?我认为这将是类似于 AWTEvent.consume()的东西。如果是这样,那么我会这样做,以便Google CodePro看不到这个unlogged。

  • 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".

推荐答案


  • 有没有一种可以调用的方法会告诉我,在调用它之前,Integer.parseInt()会抛出一个NumberFormatException吗?那么我记录下来就没有问题,因为它永远不会发生。

  • 可悲的是,至少不在核心Java API中。很容易写一个,但是 - 只需修改下面的代码。

    Sadly, no. At least not in the core Java API. It's easy to write one, however - just modify the code below.


    • 如果我根本没有抓到异常,分配?然后,我将简单地将其初始化为我不想要的值,而不是捕获异常。

    如果不捕获异常,然后堆栈将展开,直到它碰到一个将处理它的catch块,或者它将完全展开并停止线程。实际上,该变量不会被分配,但这不是你想要的。

    If you do not catch the exception then the stack will unwind until it hits a catch block that will handle it, or it will unwind completely and halt the thread. The variable will, in fact, not be assigned but this is not exactly what you want.


    • 有没有办法以异常方式标记异常明确地说我不在乎呢?我在想这将是类似于AWTEvent.consume()的东西。如果是这样,那么我会这样做,以便Google CodePro没有看到这是unlogged。

    可能有一种方式告诉CodePro忽略这个特定的警告。当然可以使用FindBugs和Checkstyle这样的工具,可以在特定位置关闭警告。 (编辑:@Andy已经指出了如何做到这一点。)

    There may be a way to tell CodePro to ignore this particular warning. Certainly with tools like FindBugs and Checkstyle you can turn off warnings in specific locations. ( @Andy has pointed out how to do this.)

    我怀疑你想要的是像@daveb提到的Commons lang包。写这样的函数很容易:

    I suspect what you want is something like the Commons lang package mentioned by @daveb. It's pretty easy to write such a function:

    int parseWithDefault(String s, int def) {
        try {
            return Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
            // It's OK to ignore "e" here because returning a default value is the documented behaviour on invalid input.
            return def;
        }
    }
    

    这篇关于在预期时处理NumberFormatException的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆