在私有实用程序类构造函数中使用的首选Throwable是什么? [英] What is the preferred Throwable to use in a private utility class constructor?

查看:138
本文介绍了在私有实用程序类构造函数中使用的首选Throwable是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有效的Java(第二版),第4项,讨论使用私有构造函数以实施非保密性。下面是本书的代码示例:

Effective Java (Second Edition), Item 4, discusses using private constructors to enforce noninstantiability. Here's the code sample from the book:

public final class UtilityClass {
    private UtilityClass() {
        throw new AssertionError();
    }
}

但是, AssertionError 似乎不是正确的事情抛。没有什么是断言,这是API如何定义 AssertionError

在这种情况下通常有不同的 Throwable 吗?一个人通常只是抛出一个通用的异常消息?或者是为此编写自定义异常的常见做法吗?

Is there a different Throwable that's typically in this situation? Does one usually just throw a general Exception with a message? Or is it common to write a custom Exception for this?

这很简单,

推荐答案

有一个断言:我主张这个构造函数将永远不会被调用。所以, AssertionError 在这里是正确的。

There is an assertion: "I'm asserting that this constructor will never be called". So, indeed, AssertionError is correct here.

这篇关于在私有实用程序类构造函数中使用的首选Throwable是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆