7

I am doing a code review and I have came across this method definition:

public void something() throws RuntimeException

Is there a rational reason to write 'throws RuntimeException' in Java?

3
  • 1
    @FeatheredOrcian it's not a dup. OP's intention is why would you add the throws RuntimeException in a method when these are unchecked exceptions. Commented Mar 21, 2015 at 16:19
  • see also Please explain RuntimeException in Java and where it should be used Commented Mar 21, 2015 at 16:31
  • @ElliotFrisch, the "duplicate" you've closed this against doesn't even mention the syntax of putting throws RuntimeException in a method signature, let alone explain the meaning of such syntax, let alone advise on when you would wish to use it. How can it possibly provide an answer to this question? Commented Jun 30, 2023 at 12:21

5 Answers 5

12

RuntimeException is unchecked exception and therefore can be thrown from any place in the code. Saying "Hey, this method can throw RuntimeException" and knowing that any piece of code can do so may be redundant. The only reason I can think of, you would like to make it explicit is for documentation purposes, eg. "Throws RuntimeException when some specific thing happens", but then it probably belongs to javadoc, not to method signature.

Sign up to request clarification or add additional context in comments.

Comments

5

Everything is context-dependent, of which you shared none; but on a general note in may be said that this very rarely makes sense. Methods declare unchecked exceptions for documentation purposes, but these are normally specific exceptions carrying some domain-specific meaning. Declaring to throw a completely generic unchecked exception is thus quite hard to justify, although I cannot say there is absolutely no situation where it may make sense.

Comments

0

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.

Comments

0

This types of exceptions depend directly of code, for example, "ArrayIndexOutOfBoundsException".So, if you don't use arrays, arraylists, casting or something what could throw a RuntimeException, it wouldn't be neccesary to write that.

Comments

0

Any method can throw RuntimeException, like NullPointerException being most common. So if a method throws RuntimeException (or it's subclass), it's usually not put that in method signature.

So according to me, it just shows inmaturity of the programmer as it doesn't add any value. Though, it's not asked but even the method name i.e. something() is fishy. It doesn't convey any meaning to the reader.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.