【问题标题】:An exception is not thrown using throw command使用 throw 命令不会抛出异常
【发布时间】:2018-03-05 17:28:57
【问题描述】:

我正在编写一个返回 FragmentManager 实例的方法,如下面的代码所示。 问题是,如果传递给方法的上下文为空,我想抛出异常,然后终止应用程序。

发生的情况是,当我将 null 传递给下面提到的方法时,应用程序关闭但 NullPointerException 中的消息是:

getFragmentManagerInstance: Context reference is null

不显示

请告诉我如何抛出异常并正确终止应用程序。

public static FragmentManager getFragmentManagerInstance(Activity activity) throws Exception {

    try {
        if (activity != null) {
            return activity.getFragmentManager();
        } else {
            throw new NullPointerException("getFragmentManagerInstance: Context reference is null");
        }
    } catch (NullPointerException e) {
        System.exit(1);
        return null;
    }
}

【问题讨论】:

  • 为什么要抛出异常?为什么不直接终止?
  • @AndyTurner 我认为这是在终止应用程序之前显示异常的好方法..但是如果我不会抛出一个接收并只是终止应用程序,我该如何打印一条日志消息到那么用户呢?
  • 不要抓住它。让它传播起来。
  • @user2121 显示异常的最佳方式是new WhateverExceptionType().printStackTrace(),或类似的。您可以创建异常而不抛出它们。
  • @user2121 你应该在catch块中记录异常,在System.exit()之前,异常堆栈跟踪不会被魔法打印出来。

标签: java android exception exception-handling nullpointerexception


【解决方案1】:

只需删除 try 块。只需输入

    if (activity != null) {
        return activity.getFragmentManager();
    } else {
        throw new NullPointerException("getFragmentManagerInstance: Context reference is null");
    }

会做你想做的,因为NullPointerException 是一个未经检查的异常。

【讨论】:

  • 如果目标是在值为 null 的情况下终止应用程序,那么这 不是 OP 想要的。这个 NPE 可以在更高的层被捕获。 (并不是说我同意这是 OP 应该想要的)。
【解决方案2】:

消息“getFragmentManagerInstance: Context reference is null”正在存储在 e 中。 您需要将其打印出来才能显示在屏幕上。

在catch块中,在System.exit(1)前添加打印语句

catch (NullPointerException e) {
        System.out.println(e);
        System.exit(1);
        return null;
}

【讨论】:

    【解决方案3】:

    不显示

    当然,那是因为你吞下了异常:

    } catch (NullPointerException e) {
        System.exit(1);
        return null;
    }
    

    消息在e 中携带,而您没有在catch 块中使用它。


    请注意,捕获NullPointerException 几乎是永远正确的做法。在这种情况下,您可以简单地打印消息并直接终止应用程序:

    if (thing == null) {
      System.err.println("It's null!");
      System.exit(1);
    }
    

    【讨论】:

      【解决方案4】:

      只需使用e.printStackTrace()

      System.exit(1)之前

      它会按照你的意愿打印

      【讨论】:

        【解决方案5】:

        该消息未显示,因为您尚未编写任何代码来打印它。如果要显示消息,请在退出前添加e.printStackTrace();

        【讨论】:

          【解决方案6】:

          为了打印一些信息,您需要将它们提供给输出流,例如System.outSystem.err

          默认情况下,如果您调用ex.printstacktrace(),它将在 System.err 中打印异常。

          您还可以使用ex.printstacktrace(System.out) 选择将信息发送到的位置,例如文件、控制台或任何输出。

          此外,您的应用程序将在 System.exit 之后立即停止,因此您的代码行需要在退出之前。

          【讨论】:

            【解决方案7】:

            我很惊讶这还没有说明,将您的 catch 块更改为

            } catch(NullPointerException e){
                System.err.print(e.getMessage());
                System.exit(1);
                return null;
            }
            

            如果您想向用户打印一条消息,请考虑使用Toast 而不是异常消息。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-01-19
              • 2019-07-05
              • 1970-01-01
              相关资源
              最近更新 更多