【问题标题】:Android - Try Catch is not catching the exception and it's not displaying my toast messageAndroid - Try Catch 没有捕捉到异常,也没有显示我的 toast 消息
【发布时间】:2014-03-08 16:06:06
【问题描述】:

我是 Android 编程新手,正在学习 Coursera 课程。在我的作业应用程序中,当用户未输入或输入不正确的时间信息 (HH:MM:SS) 时,我试图捕捉任何异常。

我的应用程序在用户出错后立即崩溃,而不是显示我的 Toast 消息或向 Logcat 显示日志消息。

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try {
    Date dt = formatter.parse(MileTime);
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    int hours = cal.get(Calendar.HOUR);
    int minutes = cal.get(Calendar.MINUTE);
    int seconds = cal.get(Calendar.SECOND);

    int duration = 3600 * hours + 60 * minutes + seconds;
    int steps_per_second = 3;

    int running_rate = duration * steps_per_second;

    //pleaseStop = false; //DouglasZare example, don't need this.
    mHandler = new Handler(); // .os package class when importing
    mLeftfoot = findViewById(R.id.leftfoot);
    mRightfoot = findViewById(R.id.rightfoot);     
    mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot); //this looks to the foot.xml file for the animations
    stepRecursive();       
} catch (ParseException e) {
    e.printStackTrace();
    Log.e(TAG, "Invalid Mile Time Entry", e);
    Toast.makeText(Assignment3MainActivity_V3_DouglasZare_AnimationCancel.this,
    "Please Enter Valid Time Stamp HH:MM:SS", Toast.LENGTH_LONG).show();
}

错误日志在这里:http://pastebin.com/By7FWxLk

这个错误非常合理,而且是故意的。

Caused by: java.text.ParseException: Unparseable date: ""

我该如何解决才能让我的 catch 语句防止这种情况发生?

【问题讨论】:

    标签: java android exception try-catch


    【解决方案1】:

    已修复。啊。我将 ParseException 更改为 Exception。这对我来说没有任何意义...我想捕获的异常是 ParseException。

    catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "Invalid Mile Time Entry", e);
            Toast.makeText(Assignment3MainActivity_V3_DouglasZare_AnimationCancel.this,
                    "Please Enter Valid Time Stamp HH:MM:SS", Toast.LENGTH_LONG).show();
        }
    

    【讨论】:

    • 表示抛出的异常不是ParseException。检查日志中的异常类型并相应更改代码
    • 捕获每个单独的异常实际上是最佳实践 - 所以首先尝试捕获ParseException,然后尝试捕获IllegalStateException。如果您想忽略最佳实践,那么您应该了解Throwable,所有Exceptions 的父类。
    • 谢谢菲尔。我将尝试使用 | 捕获每个单独的异常在每个异常之间。
    • @StacyM:+1 表示不接受您自己的答案并寻找正确的解决方案而不是修复 :)
    【解决方案2】:

    原因是抛出的异常是IllegalStateException 而不是ParseException

    如果您查看崩溃日志的第 5 行,您会看到传递给您的异常是 IllegalStateException。这是因为 ParseException 被捕获,然后IllegalStateException 被重新抛出。

    这是多个 catch 块的示例。

    try {
        //some code that can throw an exception
    } catch (IllegalStateException e) {
        //catch the IllegalStateExeption
    } catch (Exception e) {
       //catch all the ones I didn't think of.
    }
    

    【讨论】:

    • 那么,顺序重要吗?我认为我的 ParseException 仍然会捕获,因为其中一个异常是 ParseException。为了安全起见,我应该始终使用“例外”吗?
    • 这与订单无关。解析异常永远不会弥补您的代码,因为它被捕获到较低的位置。有一个通用的捕获是一个很好的解决方案,但它不是最好的。你可以有多个 catch 语句来处理所有预期的异常类型,然后在最后有一个通用的 Exception catch 来处理任何意外。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2016-01-26
    相关资源
    最近更新 更多