0

何らかの条件で入力セットを検証したい。ここで私は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
4

2 に答える 2

0

あなたがやろうとしている

 Double.parseDouble(number);

これは、Doubleに解析できる入力以外の入力では機能しません。

あなたは次のようなことをしたいかもしれません

try{
     Double.parseDouble(number);
}catch(NumberFormatException e)
{
    throw iie;
}
于 2013-01-01T20:25:17.307 に答える
0

入力した番号の種類・形式に問題があり、それが問題の根本原因でした。他のデバイスで同じコードを実行しようとすると、入力タイプ/フォーマットの問題であることが確認されます。

于 2013-03-09T17:44:37.660 に答える