guava: Add factory methods for common exceptions with formatted messages
Original issue created by electrum on 2013-04-23 at 10:31 PM
It’s common to be in a situation where precondition is known to be violated, but the condition can’t be checked for directly by a Precondition method. For example:
if (...) {
} else if (...) {
} else {
// illegal argument
}
When this occurs, we want to throw an exception with a formatted message, so we have two options:
- Use checkArgument() or checkState() with “false” as the condition
- Throw the exception and use String.format()
The first option has the advantages of being shorter and providing safety in the case of a malformed format string. But it still looks a bit ugly and seems less than ideal.
It would be nice to have factories that take Precondition-like format strings for at least the following common exceptions:
- IllegalArgumentException
- IllegalStateException
- AssertionError
- RuntimeException
- UnsupportedOperationException
About this issue
- Original URL
- State: open
- Created 10 years ago
- Comments: 21 (5 by maintainers)
Actual exception factory methods is still something being considered, but formatted messages are now possible as of 7fe1702a. CC @cpovirk.