user3221287
user3221287

Reputation: 357

Making an exception class and leaving it empty

Now I've created an exception class but haven't created a constructor for it as shown

public class InvalidChoiceException extends Exception {
}

but the catch block for that handles what I want to display.

try {
    if(readFile.hasNextInt() == false)
        throw new InputMismatchException();
    else {
        num = readFile.nextInt();
        readFile.nextLine();
    }
}  catch (InputMismatchException e) {
    System.err.println("Integer expected");
    e.printStackTrace();
    System.exit(0);
}

Is this ok to do?

edit: Let's just pretend input mismatch was a empty exception class even though it's not..

Upvotes: 0

Views: 1237

Answers (1)

nivox
nivox

Reputation: 2130

Creating an empty subclass of Exception is certainly allowed. The only catch is that you will not be able tag any instance with an error message or cause (these are the standard piece of information that are usually associated to an Exception).

However if your use case doesn't require you to specify messages or causes of your exceptions you are good with such a design.

Upvotes: 1

Related Questions