Add factory methods for common exceptions with formatted messages
See original GitHub issueOriginal 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
Issue Analytics
- State:
- Created 9 years ago
- Comments:21 (5 by maintainers)
Top Results From Across the Web
Formatting Exception Messages - Ross Tuck
Using static factories to format error messages can really improve your developer experience.
Read more >c# - FormattedException instead of throw new Exception(string ...
Format () is insignificant. I do agree in your overall approach to simplifying the code used to throw exceptions with a formatted string...
Read more >Get Started with Custom Error Handling in Spring Boot (Java)
Learn how to implement custom error handling logic in Spring Boot. You will see two approaches based on the @ControllerAdvice annotation.
Read more >MessageFormat (Java Platform SE 7 ) - Oracle Help Center
The factory methods aren't necessary because MessageFormat itself doesn't implement locale specific behavior. Any locale specific behavior is defined by the ...
Read more >The Factory Method Pattern and Its Implementation in Python
You'll also see a general purpose implementation of Factory Method in Python. ... Any other format specified is not supported, so a ValueError...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Actual exception factory methods is still something being considered, but formatted messages are now possible as of 7fe1702a. CC @cpovirk.
(But there is also the general issue of whether such a small improvement justifies all the code-review comments about whether to use that API, even if some tools add review comments to suggest the API automatically.)