Я изучаю исключения и обработку исключений, но меня немного смущает, правильно ли я делаю. ниже приведен конструктор, над которым я работаю, который выдает исключения IllegalArgumentExceptions, если параметры не совпадают.
/**
* Initializes this chair to the specified fields
*
*
* @param chairManufCost the chair manufacturing cost
* @param chairShape the chair shape
* @param chairColor the chair color
*
* @pre.
* <p> <strong> Precondition </strong> </p>
* <p> The chair color should be one of the following choices ,
* <strong><code>{black or white} </code></strong>,</p>
* <p><strong>It should be noted that colors
* string comparisons are carried out with case insensitive
* For example, these strings are equals "black", "Black", "BLACK",...</strong> </p>
*
* <p>
* The chair shape should be one of the following choices
* <strong><code>{Rectangle or Square} </code></strong>,</p>
* <p><strong>It should be noted that shapes
* string comparisons are carried out with case insensitive
* For example, these strings are equals "Rectangle", "RECtangle", "RECTANGLE",...</strong> </p>
* <p> and, the chair manufacturing cost should be positive real number
* </p>
*
* @throws IllegalArgumentException
* <p> if the manufacturing Cost of chair is negative value </p>
*
*
* @throws IllegalArgumentException
* <p>if the chair color is not one of possible chair colors</p>.
*
* @throws IllegalArgumentException
* <p> if the chair shape is not one of the possible chair shapes </p>
*
*
*
*
*
*
*
*/
public Chair(double chairManufCost, String chairShape, String chairColor) {
// COMPLETE THIS
try {
this.chairManufCost = chairManufCost;
}
catch(IllegalArgumentException e)
{
System.out.print("The manufacture cost cannot be a negative value");
}
try{
this.chairShape = chairShape;
}
catch(IllegalArgumentException r)
{
System.out.print("The chair shape must be either Square or Rectangle");
}
try {
this.chairColor = chairColor;
}
catch(IllegalArgumentException t)
{
System.out.print("the chair color must either be black or square");
}
numChairs++;
}
далее идет тест Junits, который я запускаю, чтобы проверить его, но получаю сообщение об ошибке «java .lang. AssertionError: Ожидаемое исключение: java .lang.IllegalArgumentException ", я не уверен, правильно ли я делаю это, не мог бы кто-нибудь объяснить или попытаться помочь мне выяснить, что я делаю неправильно, пожалуйста, и спасибо.
@Test(expected = IllegalArgumentException.class)
public void test03a_ctor() {
Chair ch = new Chair(120,"Cylinder", "black" );
}