1

I have a method that should only accept strings of a certain format. When the format is invalid, I want to throw an Exception. I'm quite new to Java, so am not sure what the correct type of exception to use here is. I found there is a IllegalFormatException, which based on the name sounds right, though the docs make me rather unsure about this. What is the correct Exception class to use here?

The method in question is the constructor of a PropertyId class, which takes one string argument, which should match '/^p[1-9][0-9]*$/i'. It should behave essentially the same as this equivalent in Python.

I'd prefer using an Exception provided by the standard library, unless there really is none that is appropriate, or it is generally agreed on that in my case a new Exception derivative should be created.

Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79
  • 1
    can you write your format? – KhAn SaAb Oct 05 '13 at 19:03
  • 1
    IllegalArgumentException or an app-specific exception. – Dave Newton Oct 05 '13 at 19:09
  • 1
    An argument for app-specific exception is that you get more control; Eclipse (IDE), for instance, will warn if your exception needs to be handled everywhere it is used. You don't get this feature with a runtime exception like IllegalFormatException. – Thor Hovden Oct 05 '13 at 19:17

4 Answers4

3

This appear reasonable to me, from the Javadoc for IllegalFormatException

Unchecked exception thrown when a format string contains an illegal syntax or a format specifier that is incompatible with the given arguments. Only explicit subtypes of this exception which correspond to specific errors should be instantiated.

As @BasvandenBroek points out, this is appropriate for String which contain format information, rather than checking the format of a String.

However, it may be that it's parent IllegalArgumentException is best. NumberFormatException is close, but not approriate, and it's parent is also IAE.

Note: you can create your own sub-class of IllegalArgumentException

public class StringValidationFailedException extends IllegalArgumentException {
     public StringValidationFailedException(String message) {
          super(message);
     }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

I am with @TwoThe. IllegalArgumentException is the way to go. In fact, I actually don't think there should be much debate. This situation is exactly what IllegalArgumentException was made for.

IllegalFormatException is misleading for sure, but that (and its subclasses) is for a completely different case--when the format string you provide for output is invalid.

Generating your own custom exceptions should only be done with great care. Only when you have a special case where no standard exceptions apply. That isn't true here, and you don't want your clients to have to deal with non-standard stuff unless absolutely necessary.

Remember, with great power comes great responsibility.

Vidya
  • 29,932
  • 7
  • 42
  • 70
  • In addition you can always add a specific error description to the exception during construction. – TwoThe Oct 06 '13 at 10:01
2

Try IllegalArgumentException.

TwoThe
  • 13,879
  • 6
  • 30
  • 54
1

There are many exceptions in Java, but it sounds like you may need a custom exception depending on what you mean by format. There are many examples of that on this site. Here are just two links.

How to create a custom exception type in Java?

How to define custom exception class in Java, the easiest way?

Community
  • 1
  • 1
Dom
  • 1,687
  • 6
  • 27
  • 37