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.