How liberally should I use @throws in my javadoc, or any API? For instance, if I have:
LDAPException
NullPointerException
IllegalArgumentException
Exception
Should I be using @throws for each one, or should I just say @throws Exception?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ideally, the javadoc @throws clauses should be a superset of your actual method
throwsclause.For example, if your method
throws IOException, you may not only have a@throws IOExceptionto explain situations when a generic I/O error occurs, but also@throws FileNotFoundException(a subclass ofIOException) to talk about why that may happen.Likewise, you may document
@throws NullPointerExceptionto state when this may occur, even if you don’t declarethrows NullPointerException, since it’s an unchecked exception and does not need to be (and generally is not) written in the method signature.Should you
@throws Exception? No, but, more generally you should notthrows Exception. That’s a different discussion. But as a result, no, it would not be good to repeat this sort of bad practice in javadoc. What would it talk about — the different exceptions that can occur? then why not document them one by one with@throwsanyway?