DEV Community

Kyle Carter
Kyle Carter

Posted on • Edited on • Originally published at blog.scaledcode.com

2

Effective Java: Document All Exceptions Thrown By Each Method

Documentation is critical for quickly understanding a class and its methods. Exceptions, both checked and unchecked, are part of the contract of a method and thus should be properly documented to allow users of your code to quickly understand how your code behaves.

Part of what makes good documentation is that it is very specific. While it is not a lie to document that a method throws Exception or even Throwable it isn't useful. We should be striving to throw (and by extension document) the most specific exception type possible. This is of particular import when we are documenting an interface where the interface's documentation will serve as the common documentation for all the implementations.

JavaDoc offers a simple way to document exceptions thrown by a method by using the throws tag. This first-class support for exceptions in JavaDoc can be used for both checked as well as unchecked exceptions. A particular thing that can be extra beneficial about documenting these exceptions can turn into the documentation of the preconditions for the method which can be extremely useful for users of your class. If a particular exception will be thrown from each method in a class you can choose to use a class level comment which can clean up the documentation to an extent.

Documentation is extremely important. We may not always see the immediate payoff of this documentation but in the grand scheme of things, it can pay for itself many times over.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay