【问题标题】:Will catch statement be executed if try statement is running fine?如果 try 语句运行正常,catch 语句会被执行吗?
【发布时间】:2017-10-16 17:40:29
【问题描述】:

我在 try 语句中编写了一些代码,在调试过程中,代码运行良好。 但是在 try 语句之后,控制转移到 catch 语句并且它抛出空指针异常。这是代码: -

【问题讨论】:

  • 请张贴您的代码,而不是您的代码图片。
  • 重新编译或清理代码。
  • 看看你的异常 StackTrace,它应该告诉你哪一行抛出了异常。
  • test.log 有什么作用?你能在日志中看到“点击唱歌链接”的文字吗?如果不是,那么这可能是问题所在。测试变量可能为空。
  • 如果testnull,您将在trycatch 中导致NPE。

标签: java exception-handling nullpointerexception try-catch catch-block


【解决方案1】:

只有当 try 大括号中的语句确实抛出异常时,才会执行 catch 大括号中的语句。

在这里,我认为您的 test var 为空,这就是您拥有该空指针的原因。

【讨论】:

    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多