6

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?

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
MaKri
  • 190
  • 1
  • 8
  • 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. – Luiggi Mendoza Mar 21 '15 at 16:19
  • see also [Please explain RuntimeException in Java and where it should be used](http://stackoverflow.com/questions/3540613/please-explain-runtimeexception-in-java-and-where-it-should-be-used) – Gerold Broser Mar 21 '15 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? – Mark Amery Jun 30 '23 at 12:21

5 Answers5

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.

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
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.

angeldev
  • 1,983
  • 3
  • 18
  • 29
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.

SemperAmbroscus
  • 1,308
  • 2
  • 12
  • 23
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.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55