简体   繁体   English

Exception或其子类中的无参数构造函数

[英]No-argument constructor in Exception or its subclasses

Can somebody help me understand the real use case of no-argument constructor in the context of at least exception classes. 有人可以帮助我至少在异常类的上下文中理解无参数构造函数的实际用例。

EDIT: 编辑:

As most of the guys said "it helps in the context where the exception class is self explanatory ie NullPonterException , InterruptedException ." 正如大多数人所说的那样:“在异常类是自解释类的情况下(即NullPonterExceptionInterruptedException NullPonterException 。” Apart from this use case any thing else that we can think of. 除了这个用例之外,我们还能想到其他任何事情。

Some exceptions are self-explanatory, and their stack trace is all that is necessary to understand them. 有些异常是不言自明的,理解它们所需的全部堆栈跟踪。 NullPointerException is one example. NullPointerException是一个示例。

In general, a default constructor is considered to be used, in case that you want to create an object with default values. 通常,如果要创建具有默认值的对象,则认为使用默认构造函数。 Concerning default constructors and exceptions, there is already a good answer from Paul Bellora, which I totally agree, but not recommend to use. 关于默认构造函数和异常,Paul Bellora已经提供了一个很好的答案,我完全同意,但不建议使用。 The reason for is, that you as a programmer are responsible for informing the user about what he did wrong so that he can enhance his input or interaction with the system. 原因是,作为程序员,您有责任通知用户有关他做错了什么,以便他可以增强其输入或与系统的交互。 Only displaying a dialog which tells the user 'NullPointerException' is just enough for a hello world example! 仅显示一个告诉用户'NullPointerException'的对话框就足够了。 If I implement a custom exception class, I never provide a non argument constructor. 如果实现自定义异常类,则永远不会提供非参数构造函数。

I see the advantage of a default constructors in the creation of complex objects whose creation requires a lot of configuration properties. 我看到了默认构造函数在创建复杂对象时的优势,这些对象的创建需要大量配置属性。 Applying a flood of overloaded constructors or creational methods can be very confusing and is also difficult to maintain. 应用大量重载的构造函数或创建方法可能会造成很大的混乱,并且也很难维护。 Working with dependency injection is much more convenient and simplifies the configuration of the object a lot. 使用依赖项注入更加方便,并且大大简化了对象的配置。 A very important default constructor is the protected default constructor. 一个非常重要的默认构造函数是受保护的默认构造函数。 Especially for providing an API, you have to prevent the creation of an object by using the new operation from outside. 尤其是对于提供API而言,必须从外部使用新操作来防止创建对象。 The user should be restricted to create an item of the API by using the creational methods of a factory instance. 应该限制​​用户使用工厂实例的创建方法来创建API的项。 This is the way to specify the proper use of the API. 这是指定正确使用API​​的方法。 Of course, you not only will use protected no argument constructors for APIs. 当然,您不仅会为API使用受保护的无参数构造函数。 But in most cases the properties are injected, hence for instance creation no arguments are required! 但是在大多数情况下,属性是注入的,因此,例如创建不需要参数!

A special case is the java.util.Date class whose time of creation defines the value of the object. 一个特殊情况是java.util.Date类,其创建时间定义了对象的值。 I'm sure, that there are much more application examples for no arguemnt construcotrs, but the one in combination with Exceptions is probably the worst one. 我敢肯定,有更多的应用示例没有争论的构架,但是将其与Exceptions结合使用可能是最糟糕的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Firebase数据库异常:未定义无参数构造函数 - Android Firebase Database exception: not define a no-argument constructor 无参构造函数调用2参数构造函数 - No-argument constructor calling 2-argument constructor Java无参数构造函数:抛出不可能的异常,或者有空的catch块? - Java no-argument constructor: Throw impossible exception, or have empty catch block? 隐式调用超类中的无参数构造函数 - Calling no-argument constructor in superclass implicitly 强制超类包含无参数构造函数 - Force superclasses to include a no-argument constructor kafka 流异常找不到 org.apache.kafka.common.serialization.Serdes$WrapperSerde 的公共无参数构造函数 - kafka streams exception Could not find a public no-argument constructor for org.apache.kafka.common.serialization.Serdes$WrapperSerde 在编译时强制存在无参数构造函数(Java) - Enforce presence of no-argument constructor at compile time (Java) 为无参数构造函数的类创建动态代理 - Create a dynamic proxy for a class without no-argument constructor Firebase数据库:尽管存在错误,但仍出现“无参数构造函数”错误 - Firebase Database: Getting “No-Argument Constructor” Error Despite Having It Scala类的最终def this()声明没有公共的无参数构造函数 - Scala class final def this() declares no public no-argument constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM