【问题标题】:NullPointerException in trycatch blocktrycatch 块中的 NullPointerException
【发布时间】:2016-06-22 08:19:46
【问题描述】:

我正在研究 try-catch 块。 这里我们通过 blowup() 抛出 NullPointerException , 甚至我们可以赋值

Exception e = new NullPointerException();

BlewIt 类又是一个类型 Exception 类。 所以我们抛出的异常必须在 catch 块中被捕获,但它没有。

class BlewIt extends Exception {
    BlewIt() { }
    BlewIt(String s) { super(s); }
}
class Test {
    static void blowUp() throws BlewIt {
        throw new NullPointerException();
    }
    public static void main(String[] args) {
        try {
            blowUp();
        } catch (BlewIt b) {
            System.out.println("Caught BlewIt");
        } finally {
            System.out.println("Uncaught Exception");
        }
    }
}

输出:

Uncaught Exception
Exception in thread "main" java.lang.NullPointerException
        at Test.blowUp(Test.java:7)
        at Test.main(Test.java:11)

但是如果你写这样的代码,它工作正常:

try {
    blowUp();
    } catch (Exception b) {
        System.out.println("Caught BlewIt");
    } finally {
        System.out.println("Uncaught Exception");
    }

现在 BlewIt 是 NullPointerException 类型,但我仍然得到相同的输出。

class BlewIt extends NullPointerException {
    BlewIt() {
    }

    BlewIt(String s) {
        super(s);
    }
}


class Test {
    static void blowUp() throws BlewIt {
        throw new NullPointerException();
    }

    public static void main(String[] args) {
        Exception e = new NullPointerException();
        try {
            blowUp();
        } catch (BlewIt b) {
            System.out.println("Caught BlewIt");
        } finally {
            System.out.println("Uncaught Exception");
        }
    }
}

请帮我弄清楚它背后的概念。

【问题讨论】:

  • 仅仅因为BlewItException 并不意味着NullPointerExceptionBlewIt 类型——它不是。
  • @TobiasBrösamle 感谢您的快速回复。明白你的意思。并尝试了一些不同的东西并添加了更多代码。
  • @TobiasBrösamle 虽然 BlewIt 现在是 NullPointerException 类型,但我仍然得到相同的输出。
  • 是的,因为我正在抛出一个超类,而不是子类....对吗?
  • 对。在您的最新示例中,BlewItNullPointerException - 但 NullPointerException 不是 BlewIt。这就是为什么它仍然不起作用。您需要抛出 BlewIt 才能使其工作,而不是超类,

标签: java exception nullpointerexception try-catch try-catch-finally


【解决方案1】:

NullPointerException 不是BlewIt 的子类。因此捕获BlewIt 不会捕获NullPointerException

如果你想捕捉BlewIt,你应该在blowUp()throw new BlewIt ()

【讨论】:

    【解决方案2】:

    简单地说,当 BlewIt 扩展 Exception 时,BlewIt 成为异常的子类,并不意味着它会捕获 NullPointerException。

    static void blowUp() throws BlewIt {
        throw new NullPointerException();
    }
    

    但是,由于 NullPointerException 不是已检查的异常,因此您的代码可以编译。

    但是当您稍后在代码中抛出 NullPointer 异常并尝试通过 catch 块捕获它时,正如预期的那样,它不会捕获它。

    【讨论】:

      【解决方案3】:

      您正在抛出NullPointerException,而您的catch 块正在捕获BlewIt,因此它直接转到finally 块并打印Uncaught Exception,堆栈跟踪为NullPointerException

      您的 catch 块只会捕获 BlewIt 类型及其子类型的异常。

      编辑

      class BlewIt extends NullPointerException
      

      这将使BlewIt 成为NullPointerException 的子类,反之亦然。所以你仍然无法通过BlewIt 捕捉到NullPointerException

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-08
        • 2018-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        相关资源
        最近更新 更多