对代码的几个部分进行相同的异常处理。 Java Android

时间:2014-01-09 17:31:05

标签: java android exception exception-handling

我创建了当EditText包含错误(或空)数据字符串时抛出的异常。

看起来像这样

private class NumberInputEditTextExeception extends NumberFormatException {
    public EditText editText;

    public NumberInputEditTextExeception (EditText editText) {

        this.editText = editText;
    }
}

抛出异常的方法:

private int getIntFromEditText (EditText et) throws NumberInputEditTextExeception  {
    try {
        return Integer.parseInt(et.getText().toString());
    } catch (NumberFormatException numberFormatException) {
        throw new NumberInputEditTextExeception(et);
    }
}

还有处理它的代码,并调用方法来显示“错误动画”

private long getEndTimeFromEditTexts () {
    int seconds = 0;
    int minutes = 0;
    int hours   = 0;

    try {
        seconds = this.getIntFromEditText(this.secondsEditText);
    } catch (NumberInputEditTextExeception e) {
        this.setErrorAnimationOnView(e.editText);
    }

    try {
        minutes = this.getIntFromEditText(this.minutesEditText);
    } catch (NumberInputEditTextExeception e) {
        this.setErrorAnimationOnView(e.editText);
    }

    try {
        hours   = this.getIntFromEditText(this.hoursEditText);
    } catch (NumberInputEditTextExeception e) {
        this.setErrorAnimationOnView(e.editText);
    }

    long endTimeMs = System.currentTimeMillis() + (seconds * 1000) + (minutes * 1000 * 60) + (hours * 1000 * 60 * 60);

    return endTimeMs;
}

无论如何我可以压缩重复的行:

try {
    seconds = this.getIntFromEditText(this.secondsEditText);
} catch (NumberInputEditTextExeception e) {
    this.setErrorAnimationOnView(e.editText);
}

是否有任何语法可以在抛出异常时执行不会超出整个范围(由花括号分隔)的内容?所以我可以做这样的事情

specialTry () {
    seconds = this.getIntFromEditText(this.secondsEditText); // if that throws exception, it will call "catch" block and go to the next line. NOT break out of the scope.
    minutes = this.getIntFromEditText(this.minutesEditText);
    hours   = this.getIntFromEditText(this.hoursEditText);
} catch (NumberInputEditTextExeception e) {
    this.setErrorAnimationOnView(e.editText);
}

或者我偶然发现了这个错误,因为我的设计非常糟糕,我应该改变我的设计?

2 个答案:

答案 0 :(得分:2)

您可以使用常规代码流,而不是使用异常来触发代码的执行。像下面的东西

interface EditTextErrorHandler {
    void onError(EditText et);
}

private int getIntFromEditText (EditText et, EditTextErrorHandler handler) {
    try {
        return Integer.parseInt(et.getText().toString());
    } catch (NumberFormatException numberFormatException) {
        handler.onError(et);
        return 0;
    }
}

private long getEndTimeFromEditTexts () {

    EditTextErrorHandler handler = new EditTextErrorHandler() {
        @Override
        public void onError(EditText et) {
            setErrorAnimationOnView(et);
        }
    };
    int seconds = this.getIntFromEditText(this.secondsEditText, handler);
    int minutes = this.getIntFromEditText(this.minutesEditText, handler);
    int hours   = this.getIntFromEditText(this.hoursEditText, handler);
    // values will be 0 when an error occured and the handler code is executed for each
}

答案 1 :(得分:0)

是的,你可以。如果您更改getIntFromEditText方法以使其不会引发异常,但只返回0以查找错误,则可以压缩此代码。新方法:

private int getIntFromEditText (EditText et) {
    try {
        return Integer.parseInt(et.getText().toString());
    } catch (NumberFormatException numberFormatException) {
        return 0;
    }
}

现在,您的getEndTimeFromEditTexts方法看起来更像这样:

private long getEndTimeFromEditTexts () {
    int seconds = 0;
    int minutes = 0;
    int hours   = 0;

    seconds = this.getIntFromEditText(this.secondsEditText);
    minutes = this.getIntFromEditText(this.minutesEditText);
    hours   = this.getIntFromEditText(this.hoursEditText);

    long endTimeMs = System.currentTimeMillis() + (seconds * 1000) + (minutes * 1000 * 60) + (hours * 1000 * 60 * 60);

    return endTimeMs;
}

最终结果将与您的代码完全相同。