use*_*617 5 java constructor exception
在为学校项目开设课程(如学校课程)数据库系统时,我偶然发现了一个争论问题.
我有一个名为Course的课程.这是一个类的构造函数(另一个是为空值构造函数指定默认值):
public Course(String name, String code, char level, int academicYear)
{
serialNumber = nextSerialNumber++;
if (name == null)
{
throw new NullPointerException("Name can not be null.");
}
else
{
this.name = name;
}
if (code == null)
{
throw new NullPointerException("Code can not be null.");
}
else
{
this.code = code;
}
if (indexOf(level, VALID_LEVEL) == -1)
{
throw new InvalidLevelException("Level must be one of "
+ "characters defined in the public array in Course.");
}
else
{
this.level = level;
}
if (String.valueOf(academicYear).length() != NUMBER_OF_DIGITS_IN_YEAR)
{
throw new InvalidYearException("Year must be a four digit number!");
}
else
{
this.academicYear = academicYear;
}
}
Run Code Online (Sandbox Code Playgroud)
哪里
InvalidLevelException
Run Code Online (Sandbox Code Playgroud)
和
InvalidYearException
Run Code Online (Sandbox Code Playgroud)
是作为子类的自定义异常
RuntimeException
Run Code Online (Sandbox Code Playgroud)
我从该构造函数中抛出异常以指示是否有任何错误.例如,如果在读取数据文件时遇到错误数据,我可以拒绝它并将其写入日志(如项目所要求的),只需将该构造函数放在try-catch块中然后捕获这些异常即可,并在该catch块内,记录不良数据.
在向我的老师展示这段代码之后,他说从构造函数中抛出异常是不合适的.但是,我已经阅读了许多Stackoverflow帖子,其中鼓励这种做法.
我的问题是:从上面的构造函数中抛出异常是否可以?
非常感谢附有答案的信誉良好的来源(例如,官方文件或权威书籍).
首先十分感谢.
我没有看到在构造函数中抛出异常有什么问题。这意味着该对象无法以有效状态创建。
就个人而言,您会抛出正确的异常,但如果您可以使用 Java 异常,则不要使用自定义异常InvalidLevelException,并且如果参数为 null InvalidYearException,则应将其替换为IllegalArgumentExceptionwhileNullPointerException是正确的异常。
我要改变的另一件事是风格:检查你的论点,然后做其他事情。
public Course(String name, String code, char level, int academicYear)
{
if (name == null) {
throw new NullPointerException("Name can not be null.");
}
if (code == null) {
throw new NullPointerException("Code can not be null.");
}
if (indexOf(level, VALID_LEVEL) == -1) {
throw new InvalidLevelException("Level must be one of "
+ "characters defined in the public array in Course.");
}
if (String.valueOf(academicYear).length() != NUMBER_OF_DIGITS_IN_YEAR) {
throw new InvalidYearException("Year must be a four digit number!");
}
serialNumber = nextSerialNumber++;
this.code = code;
this.academicYear = academicYear;
this.level = level;
this.name = name;
}
Run Code Online (Sandbox Code Playgroud)
(ps如果无法创建对象,为什么要增加序列号?)
非常优雅吧?——另一件事是让信息更加具体。
不管怎样,我认为最好的来源是整个 JDK 平台,因为在构造函数中抛出异常是一种常见的模式。
正如 Luiggi Mendoza 在评论中所说,如果你的老师需要一位教授,那么它就是HashMap建设者
187 public More ...HashMap(int initialCapacity, float loadFactor) {
188 if (initialCapacity < 0)
189 throw new IllegalArgumentException("Illegal initial capacity: " +
190 initialCapacity);
191 if (initialCapacity > MAXIMUM_CAPACITY)
192 initialCapacity = MAXIMUM_CAPACITY;
193 if (loadFactor <= 0 || Float.isNaN(loadFactor))
194 throw new IllegalArgumentException("Illegal load factor: " +
195 loadFactor);
196
197 // Find a power of 2 >= initialCapacity
198 int capacity = 1;
199 while (capacity < initialCapacity)
200 capacity <<= 1;
201
202 this.loadFactor = loadFactor;
203 threshold = (int)(capacity * loadFactor);
204 table = new Entry[capacity];
205 init();
206 }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |