103

For example, many methods in frameworks/JDK might throw

java.lang.SecurityException 

but this is not indicated in the method signature (since that is a practice normally reserved for checked exceptions). I want to argue that declaring RuntimeExceptions in method sigs has many benefits (akin to static type checking for example). Am I drunk or otherwise?

Svante
  • 50,694
  • 11
  • 78
  • 122
Jacques René Mesrine
  • 46,127
  • 27
  • 66
  • 104

7 Answers7

81

I would not declare an unchecked exception in the signature, since it is misleading to the user of that API. It is no longer obvious whether the exception has to be explicitly handled.

Declaring it in the javadoc is a better approach since it allows someone to handle it if they think it is necessary, but knowing they can ignore it if they want. This makes the separation between checked and unchecked clear.

Robin
  • 24,062
  • 5
  • 49
  • 58
  • 7
    The java compiler doesn't force you, to handle declared RuntimeExceptions. So you can declare them, for the purpose as a "hint" for developers. It's discussable, if JavaDoc is a better place for that. The point about checked and unchecked Exceptions is important. Checked and unchecked Exceptions are that, what Java really means with (Compiletime-)Exceptions (checked) and RuntimeExceptions (unchecked). – Guardian667 Jul 08 '16 at 11:49
  • 7
    In Spring declaring unchecked exceptions in method signature started to be a common practice. – johnlemon Aug 05 '16 at 07:20
46

From the Oracle Java tutorial:

"If it's so good to document a method's API, including the exceptions it can throw, why not specify runtime exceptions too?" Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
20

Take a look at the javadoc for Collection#add

There's a whole slew of unchecked exceptions mentioned:

Throws:
UnsupportedOperationException - add is not supported by this collection.
ClassCastException - class of the specified element prevents it from being added to this collection.
NullPointerException - if the specified element is null and this collection does not support null elements.
IllegalArgumentException - some aspect of this element prevents it from being added to this collection.

If you have the patience, I'd recommend thoroughly documenting the possible exceptions thrown by your methods this way. In a way, it's even more important to do this for unchecked exceptions, as checked exceptions are somewhat self-documenting (the compiler forces the calling code to acknowledge them).

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
  • 9
    This answer is wrong. You are talking about Javadoc statements while the question about `throws` in the method signature. These are not the same thing. Yes, you absolutely *should* document all exceptions thrown by your API, but the question isn't about that. – Gili Oct 31 '16 at 02:42
6

In my point of view it's better to declare runtime exceptions at least in the javadoc for the method. Declaring it in the signature makes it even more obvious what may happen when something goes wrong. This is my main reason for suggesting to provide this information.

FYI: as time has progressed (now in 2017) I am leaning now far more to documenting them in javadoc only and avoiding checked exceptions as much as possible.

Patrick Cornelissen
  • 7,968
  • 6
  • 48
  • 70
  • Shouldn't be avoiding unchecked exceptions instead of "avoiding checked exceptions"? – Szymon Roziewski Dec 03 '21 at 13:15
  • 1
    No, because checked Exceptions are a pain in the rear. It forces you to handle or declare them. Sometimes you can't (lambdas!), then you have to wrap it in a runtime Exception. So it's better to declare them and let the programmer decide what get's handlet where and what should propagate to the top. – Patrick Cornelissen Dec 08 '21 at 12:24
3

In my view unchecked exceptions should never be declared in the method signature as that is contrary to their nature.

If, however, a method is likely to throw some unchecked exceptions noting the likely circumstances in @throws in Javadoc can be helpful for others invoking the method in understanding what can go wrong. This is only useful though for exceptions that the callers is likely to be able to handle (such as a NPE due to bad input etc.)

Kris
  • 14,426
  • 7
  • 55
  • 65
3

If you are writing an api for use by others, then there is ample reason for explicit documentation of your intent in the api and there is no downside to declaring RuntimeExceptions in the method signature.

alphazero
  • 27,094
  • 3
  • 30
  • 26
1

This has to do with the discussion regarding checked exceptions. Most would agree that exceptions shouldn't be declared in methods signatures.

There is also a discussion regarding how runtime exceptions should be used. I agree with one poster that runtime exceptions should denote a programming error or a fatal condition. So there isn't much merit declaring them in the signature. Every method could potentially through one.

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Where does a parsing exception or some other data validation type exception fit in then. You are implying that you should not use checked exceptions but then limiting what unchecked exceptions are meant to be used for. – Robin May 05 '09 at 13:08
  • 1
    What I am saying is that the developer shouldn't be forced to catch checked exceptions. So there is no need to declare them in the method signature. In Java, you can transform them in runtime exceptions, as Spring is doing. I am also saying that runtime exceptions should be thrown only on non-recoverable situations. – kgiannakakis May 05 '09 at 13:23