2

Not a duplicate: although similar to other questions about assertions, this question specifically focuses on assertions vs exceptions in libraries to be distributed to 3rd parties and more specifically in a library that does not necessarily indicate a fatal condition to the applications using it.

There are many conflicting answers to other questions which have overlapping use cases, making most answers rather ambiguous to my specific situation.


I maintain Simple Java Mail and in the code I have a few places where I throw an AssertionError, indicating the state has become invalid and I as a library designer cannot guarantee proper functioning. Basically those should never happen and I included those as a failsafe / failfast.

Now a user has raised an issue, indicating to me that this use of java errors is inappropriate because it will potentially halt the entire application and anyway the library user will likely have implemented exception handling somewhere on a case by case basis (paraphrasing). This also makes sense to me.

What is appropriate for a library: AssertionErrors or Exceptions?

Also see this answer to "Is actively throwing AssertionError in Java good practice?", where Google Guava (and Joshua Bloch) seem to be in favor of Errors although it is not entirely clear if this applies to libraries as well.

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
  • 1
    An Error should be used to indicate "things are so bad, the only solution is to turn everything off and back on again." Are your problems really so bad that everything else the JVM is doing have to stop? Or is it just "my internal state is screwed up. You have to throw just my bit away, and start again"? If it's the latter, IllegalStateException would be more appropriate: you are literally saying that you are in a bad state, rather than the somewhat vague "stuff broke, badly". – Andy Turner Jun 13 '20 at 13:30
  • In my particular case I would think the latter case, so you would suggestion exceptions, correct? – Benny Bottema Jun 13 '20 at 13:32
  • Does this answer your question? [Java assertions underused](https://stackoverflow.com/questions/298909/java-assertions-underused) – Joe Jun 13 '20 at 13:40
  • @BennyBottema yes. You may find it helpful to refer to the summary table here https://github.com/google/guava/wiki/ConditionalFailuresExplained which concisely captures the philosophy of which exception types are used in Guava. – Andy Turner Jun 13 '20 at 13:42
  • I understand the referenced summary, but your answer confuses me as the summary states Assertions are used if 'I' (the library) messed up, which is also my situation. – Benny Bottema Jun 13 '20 at 13:56
  • @Joe - `assert` and explicitly throwing `AssertionError` are different things. An `assert` can be disabled at runtime. Explicit `throw new AssertionError(...)` cannot be disabled. – Stephen C Jun 13 '20 at 15:14
  • How is this opinion based @StephenC?Where will I find an answer if not on stackoverflow? – Benny Bottema Jun 13 '20 at 15:19
  • 1
    *How is this opinion based* - As you state in your question, your user thinks that throwing `AssertionError` is wrong, but Bloch says it is OK. So clearly, it is a matter or opinion. *"Where will I find an answer if not on Stackoverflow?"* - Try Quora. (What makes you think that there >>is<< a definite answer?) – Stephen C Jun 13 '20 at 15:22
  • @StephenC What makes you think there isn't? Regarding bloch, he didn't specifically state this to be for library design, so I'm not even sure it is relevant. Even if there's no black and white answer, like many answers, a definitive answer in principle would be one that highlights the major options with advantages / disadvantages. – Benny Bottema Jun 13 '20 at 15:39
  • *"What makes you think there isn't?"* - The evidence that you quoted AND my knowledge. – Stephen C Jun 13 '20 at 15:40
  • Besides, your question is essentially the same as the Question that you linked to. The "major options" are obvious (`throw AssertionError` or `throw SomeOtherException`) and the advantages / disadvantages are explained in that Q&A ... and the Q&A it is a dup of. We really don't need dups of dups .... – Stephen C Jun 13 '20 at 15:47
  • @StephenC I strongly believe the library angle has not been explored yet. The linked Q&A says nothing about the implications of throwing Errors that might affect an application you know nothing about. I would like to know if AssertionErrors are still preferred / appropriate in this context... – Benny Bottema Jun 13 '20 at 15:55

1 Answers1

0

You have the right idea. In Java you can use Exceptions to indicate when an operation cannot proceed. Examples of this are MalformedURLException, and NumberFormatException.

The pre-built exception I would point you to is IllegalStateException, but you also might way to create your out Exception, in which case you need to think about whether it should extend Exception or RuntimeException.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80