Java:如何在 Singleton 中重现 Legen...dary ClassNotFoundException?

标签 java exception singleton classnotfoundexception

在上次面试中,我被问到一个关于 java 中所有 Singleton 实现的标准问题。他们有多糟糕。

有人告诉我,由于构造函数中可能出现未经检查的异常,即使是静态初始化也是不好的:

public class Singleton {
    private static Singleton instance = new Singleton();

    public static Singleton getInstance() {
        return instance;
    }
    private Singleton() {
       throw new RuntimeException("Wow... Exception in Singleton constructor...");
    }
}

他们还告诉我,异常将是“ClassNotFoundException”,因此很难在实际应用中找到问题。

我试图得到那个异常:

public static void main(String[] args) {
    new Thread(new Runnable(){
            public void run() {
                Singleton.getInstance();
            }
        }).start();
}

但我得到的唯一信息是 ExceptionInInitializerError...

我在谷歌上搜索了那个异常,在我发现的所有地方——他们都在谈论我在面试中被告知的同一个问题。与“实现”无关 =)

感谢您的关注。

最佳答案

有时你会在面试中遇到奇怪的问题,你应该总是预料到意想不到的事情。 ;)

您使用的任何技术都有优点和缺点。您必须知道什么时候是使用它们的好时机、可能出现的问题以及如何解决它们。

重要的是要记住,几乎每个问题都有解决方案或解决方法。

On my last interview I was asked a standard question about all Singleton implementations in java. And how bad they all are.

听起来不像是一个问题,更像是一场宗教辩论。

And I was told that even the static initialization is bad because of the probability of unchecked exception in constructor:

Siderman IV:蜘蛛侠 vs Singleton 和静态初始化器。 (坏人;)

throw new RuntimeException("Wow... Exception in Singleton constructor...");

这是个坏主意,所以不要那样做。事实上它甚至不会编译。 Java 6 中的编译器报告。

initializer must be able to complete normally

And they also told me that the Exception is gonna be "ClassNotFoundException" so it would be extremely hard to find the problem in real app.

您会收到一个 ExceptionInInitializerError,其中包含导致问题的异常原因。除了必须阅读嵌套的异常之外,它并不那么棘手。

static class Inner {
    static {
        Integer.parseInt(null);
    }
}

public static void main(String... args) {
    try {
        new Inner();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    new Inner();
}

打印

Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.main(Main.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.parseInt(Integer.java:499)
at Main$Inner.<clinit>(Main.java:8)

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class Main$Inner
at Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

第一次发生这种情况,之后如果您尝试再次使用该类,您会得到 NoClassDefFoundError。

过去,当您尝试使用反射访问一个类而失败时,您会收到 ClassNotFoundException(没有关于实际发生情况的详细信息),但现在情况已不同。

关于Java:如何在 Singleton 中重现 Legen...dary ClassNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7553000/

相关文章:

java - 正则表达式忽略字母,直到出现数字,然后匹配字母

c# - 在 VS2013 的 C# 代码中查找异常隐藏/吞噬

android - 我如何格式化解析并传递给 editText 的数字?

java - 存储和加载 REST 服务器的配置,避免全局状态(即单例、上下文、依赖注入(inject))

java - 在原型(prototype) bean 中使用 Spring singleton-bean。会发生什么?

android - Kotlin 单例应用程序类

java - KeyListener实现后拒绝工作,将keylistener添加到对象并实现方法

java - android.view.InflateException : Binary XML file line #13: Error inflating class android. widget.TextView

java - 通过 java 企业应用程序调用 unix 可执行文件

java - 索引越界异常java