Should methods that throw RuntimeException indicate it in method signature?

后端 未结 7 1673
小鲜肉
小鲜肉 2020-11-30 23:43

For example, many methods in frameworks/JDK might throw

java.lang.SecurityException 

but this is not indicated in the method signature (si

相关标签:
7条回答
  • 2020-11-30 23:50

    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.

    0 讨论(0)
  • 2020-12-01 00:00

    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.

    0 讨论(0)
  • 2020-12-01 00:03

    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.

    0 讨论(0)
  • 2020-12-01 00:04

    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).

    0 讨论(0)
  • 2020-12-01 00:06

    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.)

    0 讨论(0)
  • 2020-12-01 00:08

    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.

    0 讨论(0)
提交回复
热议问题