question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add factory methods for common exceptions with formatted messages

See original GitHub issue

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:

  1. Use checkArgument() or checkState() with “false” as the condition
  2. 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:open
  • Created 9 years ago
  • Comments:21 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dimo414commented, May 15, 2018

Actual exception factory methods is still something being considered, but formatted messages are now possible as of 7fe1702a. CC @cpovirk.

0reactions
cpovirkcommented, Apr 1, 2022

(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.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found