捕获数组异常

时间:2016-12-26 10:14:59

标签: java android arrays exception exception-handling

各位大家好,并提前致谢。

我在代码数组中出错。我已阅读有关此问题的所有主题,但仍无法正确处理异常,导致我的应用程序关闭。

这是我的代码......

    public boolean HoraErronea(String HoraAValidar) throws IndexOutOfBoundsException
{
    String HoraIntroducida = HoraAValidar;
    boolean HoraErronea = false;

    if(HoraIntroducida.length() < 3 || HoraIntroducida.length() > 5)
    {
        HoraErronea = true;
    }
    else
    {
        try
        {
            HoraIntroducida = HoraIntroducida.replace("-", ":");
            String[] HoraTroceada = HoraIntroducida.split(":");

            int hora = HoraTroceada[0].equals("")?-1:Integer.parseInt(HoraTroceada[0]);
            int minuto = HoraTroceada[1].equals("")?-1:Integer.parseInt(HoraTroceada[1]);

            if(!((hora >= 0 && hora <= 24) && (minuto >= 0 && minuto <= 59)))
            {
                HoraErronea = true;
            }
        }
        catch (IndexOutOfBoundsException e)
        {
            e.printStackTrace();
        }
    }

    return HoraErronea;
}

好的,事情就是&#34; HoraIntroducida&#34;没有&#34;:&#34;,然后&#34; HoraTroceada [1]&#34;不存在,所以我的代码打破了这个消息......

  

E / MessageQueue-JNI:java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1                                                                              在com.essimple.essley.FuncionesActivity.HoraErronea(FuncionesActivity.java:366)                                                                              在com.essimple.essley.CalendarioActivity.ValidaDatos(CalendarioActivity.java:483)                                                                              在com.essimple.essley.CalendarioActivity.onBackPressed(CalendarioActivity.java:523)                                                                              在android.app.Activity.onKeyUp(Activity.java:2725)                                                                              在android.view.KeyEvent.dispatch(KeyEvent.java:2699)                                                                              在android.app.Activity.dispatchKeyEvent(Activity.java:3026)                                                                              在android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:547)                                                                              在android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:57)                                                                              在android.support.v7.app.AppCompatDelegateImplBase $ AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:315)                                                                              在com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:317)                                                                              在android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4327)                                                                              在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)                                                                              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3849)                                                                              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3902)                                                                              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3868)                                                                              在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3995)                                                                              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3876)                                                                              在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4052)                                                                              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3849)                                                                              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3902)                                                                              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3868)                                                                              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3876)                                                                              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3849)                                                                              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3902)                                                                              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3868)                                                                              在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4028)                                                                              在android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4189)                                                                              在android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2365)                                                                              在android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1961)                                                                              在android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1952)                                                                              在android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2342)                                                                              在android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)                                                                              在android.os.MessageQueue.nativePollOnce(本机方法)                                                                              在android.os.MessageQueue.next(MessageQueue.java:323)                                                                              在android.os.Looper.loop(Looper.java:136)                                                                              在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我知道我可以检查HoraTroceada的长度并防止异常,但是我希望以更好的编码行为来做,抓住异常。

那么请问,有人可以告诉我这有什么问题吗? 感谢大家的时间和关注。

5 个答案:

答案 0 :(得分:2)

要验证空引用,必须使用if语句:

Object ref = null;

if(ref != null) {

System.out.println(ref.toString());

}

而不是使用catch语句

答案 1 :(得分:1)

您的代码缺少&#34;运行时异常处理&#34;。
见如何:

 String HoraIntroducida = HoraAValidar;

让我们假设,HoraValidar只是&#34; 1&#34;。因此,HoraIntroducida只持有&#34; 1&#34; 长度是1

String[] HoraTroceada = HoraIntroducida.split(":");

由于在上述情况下,HoraTroceada中只有一个元素。

int minuto = HoraTroceada[1].equals("")?-1:Integer.parseInt(HoraTroceada[1]);

这里当您的数组中只有一个元素时,您正在访问元素编号2。所以期待那里的运行时错误。

所以,解决方案将是

if(HoraTroceada.length == 1)
    int hora = HoraTroceada[0].equals("")?-1:Integer.parseInt(HoraTroceada[0]);
if(HoraTroceada.length == 2)
    int minuto = HoraTroceada[1].equals("")?-1:Integer.parseInt(HoraTroceada[1]);

答案 2 :(得分:1)

根据您的例外:

  

java.lang.ArrayIndexOutOfBoundsException:length = 1; index = 1

HoraTroceada 只包含一个值。所以长度为1。

所以我们无法访问&#34; HoraTroceada [1]&#34;这个值。

所以在去做之前先检查一下这个角色&#34; - &#34;可用与否 像这样

if(HoraIntroducida.contains("-"){
 HoraIntroducida = HoraIntroducida.replace("-", ":");
String[] HoraTroceada = HoraIntroducida.split(":");
}

应该是完整的帮助。

答案 3 :(得分:0)

在访问索引之前,检查数组的长度是否大于0然后访问索引

if(HoraTroceada.length > 0){
    int hora = HoraTroceada[0].equals("")?-1:Integer.parseInt(HoraTroceada[0]);
        int minuto = HoraTroceada[1].equals("")?-1:Integer.parseInt(HoraTroceada[1]);
}

答案 4 :(得分:0)

正如其他答案所暗示的那样,您使用数组索引是不正确的。您应该始终测试以查看返回的对象是否符合您的预期。在您的情况下,您期望一个大小为2的数组。然后明确检查并正确处理错误情况。

一般来说,您应该始终检查所调用方法的输出。您应该适当地处理不一致,例如抛出异常或显示错误消息。检查返回的对象是否为:

  • not NULL
  • 不在您正在寻找的特定范围内
  • 不是空的
  • 属于正确类型

如果检测到不一致,则抛出异常并从方法中提前退出。否则,您冒着将无效数据传递到代码的更深层部分的风险,这可能导致数据损坏和未定义的行为。

您可以在我的文章中找到有关处理异常的更多提示:Exceptions Guidelines