Should methods that throw a RuntimeException point to a method signature?

For example, many methods within / JDK may throw

java.lang.SecurityException 

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

+51
java oop exception exception-handling
May 05 '09 at 10:13
source share
7 answers

I would not declare an unchecked exception in the signature, as it misleads the user of this API. It is no longer obvious whether there should be an explicit appeal to the exception.

Declaring it in javadoc is the best approach as it allows someone to handle it if they consider it necessary, but knowing that they can ignore it if they want. This makes the distinction between verified and unverified clear.

+42
May 05 '09 at 13:24
source share

From the Oracle Java Tutorial :

"If it is so good to document the method API, including exceptions, it can throw, why not specify runtime exceptions too?" Runtime exceptions are problems resulting from a programming problem, and as such, client-side API code cannot be reasonably expected to recover from them or process them in any way. such problems include arithmetic exceptions, such as division by zero; pointer exceptions, such as trying to access an object through null Help; and indexing exceptions, such as trying to access an array element through an index too large or too small.

Runtime exceptions can occur anywhere in the program, and in a typical they can be very numerous. The need to add runtime exceptions to each method declaration will reduce program clarity.

+20
04 Sep '12 at 18:37
source share

Take a look at javadoc for Collection # add

Many exceptions were mentioned there:

 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 patience, I would recommend carefully documenting the possible exceptions thrown by your methods in this way. In a sense, this is even more important for excluded exceptions, since checked exceptions are somewhat self-documenting (the compiler forces the calling code to recognize them).

+15
May 05 '09 at 11:28 a.m.
source share

In my point of view, it is better to declare runtime exceptions, at least in javadoc for the method. The signature ad makes it even more obvious what can happen when something goes wrong. This is my main reason for offering this information.

FYI: as time progresses (now in 2017), I am more inclined to document them only in javadoc and avoid checked exceptions as much as possible.

+4
May 05 '09 at 10:17 a.m.
source share

In my opinion, thrown exceptions should not be declared in the method signature, as this is contrary to their nature.

If, however, the method is likely to throw some uncontrolled exceptions, noting that probable circumstances in @throws in Javadoc may be useful to others who reference the method, realizing what might go wrong. This is only useful for exceptions that callers can handle (e.g. NPE due to bad write, etc.).

+3
May 05 '09 at 11:12
source share

If you are writing api for use by others, then there is reason enough to explicitly document your intent in the api, and there is no shortage of declaring RuntimeExceptions in the method signature.

+3
May 05 '09 at 11:45
source share

This is due to a discussion of checked exceptions . Most would agree that exceptions should not be declared in signature methods.

There is also discussion regarding the use of runtime exceptions. I agree with one poster that runtime exceptions should indicate a programming error or fatal condition. Thus, in the signature there is no great dignity declaring them. Each method can potentially through one.

+1
May 05 '09 at 10:30 a.m.
source share



All Articles