解析为Double时捕获numberformatException

时间:2013-01-01 20:10:23

标签: android exception number-formatting

我想验证具有某些条件的输入集。 在这里我得到NumberFormatException同时绕过号码。如果在文本框中输入的数字包含除数字之外的任何内容,我想抛出InvalidInputException。 现在,如果我只输入数字,那么我也会得到NumberFormatException。 这是示例代码。 致validateInput的电话如下

 try {
                    if (true == validateInput(name.getText().toString(), number
                            .getText().toString())) {
                        // do something
                    }
                } catch (InvalidInputException iie) {
                    Log.d("@gaurav", "not a valid input" + iie);
                    Toast.makeText(this, "Invalid input set", Toast.LENGTH_LONG)
                    .show();
                } catch (Exception ex) {
                    Toast.makeText(this, "Problem while cerating contact", Toast.LENGTH_LONG)
                    .show();
                    Log.d("@gaurav", "Problem while cerating contact", ex);
                } finally {
                 // do something    
                }

ValidateInput()如下:

 * Name is valid if it starts with an alphabet otherwise not valid Number is
 * valid if the entered text is integer only, 
 * if not valid number/name throws InvalidInputException, otherwise true
 * */
private boolean validateInput(String name, String number)
        throws InvalidInputException {
    InvalidInputException iie = new InvalidInputException();
    try {
        if (name.isEmpty() || number.isEmpty()) {
            Log.d("@gaurav.exception", "input field empty");
            iie.addDescription("Input field is empty");
            throw iie;
        } else {
            if (false == Character.isLetter(name.charAt(0))) {
                Log.d("@gaurav.exception", "first letter of name is not a letter");
                iie.addDescription("first letter of the name is not character");
                throw iie;
            }
            Log.d("@gaurav.exception", "checking number");
        Log.d("@gaurav.exception","number is :"+number+":");                  
            Double.parseDouble(number);
        }
    } catch (NumberFormatException e) {
        Log.d("@gaurav.exception", "In numberFormatexception, adding description, re-throwing iie");
        iie.addDescription("Number field should contain integer only");
        throw iie;
    }
    catch (Exception e) {
        Log.d("@gaurav.exception", "Exception, re-throwing iie");
        throw iie;
    }
    iie = null;
    return true;
}

MyCustomException如下

package com.gaurav.contactmanager.model;

public class InvalidInputException extends Exception {
    /**
     * 
     */
    private static final long serialVersionUID = 5585822470459144546L;
    String description;

    public InvalidInputException() {
        super();
    }

    public InvalidInputException(String desc) {
        this.description = desc;
    }

    public void addDescription(String desc) {
        description = desc;
    }

    @Override
    public String toString() {
        return description + "\n\n" + super.toString();
    }

}

Logcat显示如下内容:

01-02 02:11:59.310: D/@gaurav.exception(408): checking number
01-02 02:11:59.321: D/@gaurav.exception(408): number is :6666:
01-02 02:11:59.330: D/@gaurav.exception(408): In numberFormatexception, adding description, re-throwing iie
01-02 02:11:59.330: D/@gaurav(408): not a valid inputNumber field should contain integer only
01-02 02:11:59.330: D/@gaurav(408): com.gaurav.contactmanager.model.InvalidInputException

2 个答案:

答案 0 :(得分:0)

你正在尝试

 Double.parseDouble(number);

除了可以解析为Double的输入之外,哪个输入不起作用。

您可能想要执行类似

的操作
try{
     Double.parseDouble(number);
}catch(NumberFormatException e)
{
    throw iie;
}

答案 1 :(得分:0)

我输入的数字的类型/格式存在一些问题,这是问题的根本原因。尝试在其他设备上运行相同的代码,确认它是输入类型/格式问题。