-1

How do I make a custom exception to determine that a string != 1? So far this is what I have, but I'm not sure if it's anywhere close to being right.

I'm trying to program a card game, and I want to throw an exception for

if (rank.length() != 1) {
        return;
    }

So, I'd like to have an exception for that.

public class StringLengthException extends Exception{


  public StringLengthException () {}


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

}

Here's the class I'm trying to write the exception for

/**
 * 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);
        }
    }
}

3 Answers3

1

What you can do is to check if the length of the string is equal to 1, if so, throw the exception:

String str = "...";

if (str.length() == 1)
    throw new StringLengthException();

You have to put this, inside a method:

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

Don't forget that if you throw an exception inside a method, you have to declare that the method throws that exception.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

An exception is thrown with information when an exceptional situation occurs. You need to figure out exceptional condition while some operation is in progress. Like in your case, string length not equal to 1. So your code should look like:

if (yourstring.length != 1){
    throw new StringLengthException();
    }

Also an exception should contain a message, so the identification of cause becomes easier.

if (yourstring.length != 1){
    throw new StringLengthException("String length is not equal to 1: String is:" + yourstring);
    }
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
0

Your class is good, the only thing you have to do now is use it:

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

    // Do stuff
}

And call the method like that:

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

But, if you want to store just a character, why don't you use a Character or a char type ?

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47