且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是AssertionError?在这种情况下,我应该从我自己的代码中抛出?

更新时间:2021-10-21 15:48:01


当然,你不会实例化这个类的项目语句已被违反,但如果这是背后的逻辑,那么我们应该抛出 AssertionErrors 无处不在,这显然不是会发生什么。

Of course the "You shall not instantiate an item of this class" statement has been violated, but if this is the logic behind that, then we should all throw AssertionErrors everywhere, and that is obviously not what happens.

代码不是说用户不应该调用零args构造函数。这个说法就是说,就程序员而言,他/她已经使得不可能调用零参数构造函数(在这种情况下使它成为 private ,而不是从示例的代码中调用它。因此,如果发生呼叫,则该断言已被违反,因此 AssertionError 是适当的。

The code isn't saying the user shouldn't call the zero-args constructor. The assertion is there to say that as far as the programmer is aware, he/she has made it impossible to call the zero-args constructor (in this case by making it private and not calling it from within Example's code). And so if a call occurs, that assertion has been violated, and so AssertionError is appropriate.