我正在创建一个扑克客户端,我怎样才能使异常处理更干净?
I'm creating a poker client, how can I make the exception handling more clean?
我正在检查这些值是否有效。 if parts
对我来说看起来仍然很乱,检查了很多 ||
运算符,还有多个 InvalidArgumentException
,但我总是检查那个。
如何才能更干净?
这是我脚本的一部分:
public Card(String cardCode) throws IllegalArgumentException {
this.cardCode = cardCode;
String cardColor = this.cardCode.substring(0, 1).toUpperCase();
String cardValue = cardCode.substring(1).toUpperCase();
Integer intCardValue = Integer.parseInt(cardValue);
if (!colors.contains(cardColor))
{
throw new IllegalArgumentException("card color isn't valid: " + cardColor);
}
if (alphabeticCardValue.get(cardValue) == null || intCardValue > 10 || intCardValue < 2 ) {
throw new IllegalArgumentException("card number isn't valid: " + intCardValue);
}
}
谢谢
也许通过将检查放入一个单独的方法?
类似于 isValidCard()
和 isValidColor()
,因此您可以使用这些方法创建方法 isValidCard()
。
编辑:直接使用 ujulu 的回答
如果您检查构造函数中的值,您可以确保所有卡片都有效。有一个缺点 - 当使用无效参数调用构造函数时,您必须决定将创建哪个(有效)卡片。
您实际要做的是验证输入。使用 IllegalArgumentException
是不合适的,imo,因为这个异常的目的是在 JavaDoc 中定义如下:
- Thrown to indicate that a method has been passed an illegal or
inappropriate argument.
我会做的是:
为可能的颜色定义一个枚举:
public enum Color { BLACK, RED, ... }
为可能的卡片值定义枚举:
public enum CardValues {
TWO(2),
THREE(3); // ...
private int value;
private CardValues(final int v) {
value = v;
}
public getValue() { return value;}
}
修改构造函数如下:
public Card(Color color, CardValues cardValues) {
if (color == null || cardValues == null) {
throw new IllegalArgumentException("....");
}
// doSomething else
}
注意:IllegalArgumentException
是一个未经检查的异常。所以你不需要在throws
子句中指定它。
我正在检查这些值是否有效。 if parts
对我来说看起来仍然很乱,检查了很多 ||
运算符,还有多个 InvalidArgumentException
,但我总是检查那个。
如何才能更干净?
这是我脚本的一部分:
public Card(String cardCode) throws IllegalArgumentException {
this.cardCode = cardCode;
String cardColor = this.cardCode.substring(0, 1).toUpperCase();
String cardValue = cardCode.substring(1).toUpperCase();
Integer intCardValue = Integer.parseInt(cardValue);
if (!colors.contains(cardColor))
{
throw new IllegalArgumentException("card color isn't valid: " + cardColor);
}
if (alphabeticCardValue.get(cardValue) == null || intCardValue > 10 || intCardValue < 2 ) {
throw new IllegalArgumentException("card number isn't valid: " + intCardValue);
}
}
谢谢
也许通过将检查放入一个单独的方法?
类似于 isValidCard()
和 isValidColor()
,因此您可以使用这些方法创建方法 isValidCard()
。
编辑:直接使用 ujulu 的回答
如果您检查构造函数中的值,您可以确保所有卡片都有效。有一个缺点 - 当使用无效参数调用构造函数时,您必须决定将创建哪个(有效)卡片。
您实际要做的是验证输入。使用 IllegalArgumentException
是不合适的,imo,因为这个异常的目的是在 JavaDoc 中定义如下:
- Thrown to indicate that a method has been passed an illegal or inappropriate argument.
我会做的是:
为可能的颜色定义一个枚举:
public enum Color { BLACK, RED, ... }
为可能的卡片值定义枚举:
public enum CardValues { TWO(2), THREE(3); // ... private int value; private CardValues(final int v) { value = v; } public getValue() { return value;} }
修改构造函数如下:
public Card(Color color, CardValues cardValues) { if (color == null || cardValues == null) { throw new IllegalArgumentException("...."); } // doSomething else }
注意:
IllegalArgumentException
是一个未经检查的异常。所以你不需要在throws
子句中指定它。