• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to avoid RuntimeException

 
Akash Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the appropriate way to write code to avoid runtime exception ?
I have two cases, one for constructor and one for method.
Constructor:
1.
Valid values --
field : can not be blank or null
length : can not be zero or -ve.
// make sure object is in valid state after construction.
// Write in Java doc that method throws RuntimeException
// if name is passed as null or blank.


2.
// object may not be in valid state, if name and length have
// are not intialized with some valid default value.

3.
// same applies here too, default values would be overridden
// with values passed in parameter. Constructed object may not
// be valid.



Which one is appropriate way to for this constructor ?

--------
Method:
1.
// write in Java doc, method throws NullPointerException
//if null is passed.

2.
// Write in Java doc, method does not do any thing
// if null is passed.

3.
// no need to write any thing about NullPointeException,
// but good if you write it can throw NullPointerException.

Should method be robust enough to not throw RuntimeException, by checking
and throwing RuntimeException( as point 1); or proceed only when parameter(s) are valid (as point 2) by writing in Java doc do not pass incorrect value otherwise..; or just proceed with assumption that caller would always pass a correct value to proceed ?
Which one is appropriate way for this method ?

Regards,
Akash.
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akash,
I don't see much differences between constructors and methods as far as the need for checking parameters is concerned. I think that it depends of the context.
I prefer solution 1. of your constructor example and solution 3. of your method example.
As you wrote yourself, in the latter you don't need to throw NullPointerException yourself as the call on your object will do it for you anyway.
Now there may be contexts where you could hesitate between correcting a parameter value and throwing a IllegalArgumentException :


Both may be defendable IMO, as far as the method behavior is well documented.
Best,
Phil.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akash,
Any of your suggested classes constructors or methods may be reasonable - it depends on other factors.
If your class cannot possibly be used if the arguments are not valid, then you might want to throw an exception in the constructor.
If your class can be used (even if only partially) without the values being provided in the constructor, then you probably should not throw an exception.
Same goes for the methods - can they return a reasonable result if they don't have the correct arguments? If they can, then return that value, if they can't throw an exception.
As Phil said - as long as you document what your class does, you should be able to defend your choice.
Regards, Andrew
 
Akash Singh
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Andrew and Phil.
Regards,
Akash
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic