java中字符串的自定义异常

java中字符串的自定义异常,java,exception,custom-exceptions,Java,Exception,Custom Exceptions,如何创建自定义异常以确定字符串是否正确!=1. 到目前为止,这就是我所拥有的,但我不确定这是否接近正确 我正在尝试编程一个纸牌游戏,我想为它抛出一个异常 if (rank.length() != 1) { return; } /** * Name mutator. * * Business rules: - should be in the range A, 1, ..., 9, T, J, Q, K * * @param rank the rank to s

如何创建自定义异常以确定字符串是否正确!=1. 到目前为止,这就是我所拥有的,但我不确定这是否接近正确

我正在尝试编程一个纸牌游戏,我想为它抛出一个异常

if (rank.length() != 1) {
        return;
    }
/**
 * Name mutator.
 *
 * Business rules: - should be in the range A, 1, ..., 9, T, J, Q, K
 *
 * @param rank the rank to set
 */
public void setRank(String rank) {
    // make sure the rank isn't null
    if (rank == null) {
        throw new NullPointerException ("Rank is null"); 
    }
    // make sure the rank isn't too long or too short
    if (rank.length() != 1) {
        return;
    }
    rank = rank.toUpperCase();
    // check if the rank is one of the allowed ones
    if ("A23456789TJQK".contains(rank)) {
        this.rank = rank;
        // is this an ace?
        if (rank.equals(ACE)) {
            this.value = 1;
        } else // perhaps it is a face card?
        if ((TEN + JACK + QUEEN + KING).contains(rank)) {
            this.value = 10;
        } else {
            // it must be a regular card
            this.value = Integer.parseInt(rank);
        }
    }
}
所以,我想有一个例外

public class StringLengthException extends Exception{


  public StringLengthException () {}


  public StringLengthException (String message)
  {
     super(message);
  }

}
这是我试图为其编写异常的类

if (rank.length() != 1) {
        return;
    }
/**
 * Name mutator.
 *
 * Business rules: - should be in the range A, 1, ..., 9, T, J, Q, K
 *
 * @param rank the rank to set
 */
public void setRank(String rank) {
    // make sure the rank isn't null
    if (rank == null) {
        throw new NullPointerException ("Rank is null"); 
    }
    // make sure the rank isn't too long or too short
    if (rank.length() != 1) {
        return;
    }
    rank = rank.toUpperCase();
    // check if the rank is one of the allowed ones
    if ("A23456789TJQK".contains(rank)) {
        this.rank = rank;
        // is this an ace?
        if (rank.equals(ACE)) {
            this.value = 1;
        } else // perhaps it is a face card?
        if ((TEN + JACK + QUEEN + KING).contains(rank)) {
            this.value = 10;
        } else {
            // it must be a regular card
            this.value = Integer.parseInt(rank);
        }
    }
}

您可以做的是检查字符串的长度是否等于1,如果是,则抛出异常:

String str = "...";

if (str.length() == 1)
    throw new StringLengthException();
您必须将其放入一个方法中:

public void someOperationWithStrings(String str) throws StringLengthException {
    if (str.length() == 1)
        throw new StringLengthException();
}

不要忘记,如果在方法内部引发异常,则必须声明该方法引发该异常。

异常情况发生时,将引发包含信息的异常。当某些操作正在进行时,您需要找出异常情况。就像您的情况一样,字符串长度不等于1。 因此,您的代码应该如下所示:

if (yourstring.length != 1){
    throw new StringLengthException();
    }
此外,异常还应包含消息,以便更容易识别原因

if (yourstring.length != 1){
    throw new StringLengthException("String length is not equal to 1: String is:" + yourstring);
    }

你的课程很好,你现在唯一要做的就是使用它:

public void doSomething (String rank) throws StringLengthException {
    if (rank.length()!=1)
        throw new StringLengthException("rank is not of size 1");

    // Do stuff
}
然后像这样调用该方法:

try {
    String rank = "A";
    doSomething(rank);
} catch (StringLengthException sle) {
    sle.printStackTrace();
}

但是,如果您只想存储一个字符,为什么不使用
字符
字符
类型?

您正在查找
IllegalArgumentException