Often better to make sure NPEs don't happen by actively throwing them. There are some instances where null values are to be expected (eg some leaves of trees are always null), so the if (foo != null) test is necessary. In other circumstances this is betterYou should allow your application to crash, then debug it and find where the null came from and correct that error.sujith Acharya wrote:If we take the possiblity of NullPointerException, as per case i) we check if(obj != null) {//do something} else {//do something else} as per case ii) try{//do something} catch(NullPointerException e){//do something else}
There is no truth in the rumour that I am IrishI myself wrote: . . . Often better to make sure NPEs don't happen by actively throwing them. . . .
Maneesh wrote:Sometimes it makes sense to catch and process these exceptions. e.g. while validating user input for say age. In such scenarios catching process a NumberFormatException makes sense.
Stanley Walker wrote:however i have just one doubt. i know many senior associates in my project who advocate using try{} catch(Exception e){} usage. their explanation is code should never reach melting point no matter what the situation is. is this advisable?
Stanley Walker wrote:and also what would you guys suggest, should i throws Exception @ method definition or individually catch the exceptions. i prefer catching exceptions but is it a good programming practice?
Stanley Walker wrote:From a purely technical point of view, is it a good idea or even an acceptable coding standard to catch exceptions like indexoutofbounds, numberformatexceptions,nullpointerexceptions,etc... or the code should be so refined such that these errors never happen. eg: null check to handle nullpointerexceptions
I know many people would have very different view point, any sugegstion is welcome.
Check out my blog on software development: http://www.turnleafdesign.com
Please use the CODE button rather than writing the tags by hand; as you see you have misspelt them.Billy Korando wrote: . . . better to write
<code>
if(foo == null)
System.out.println("You have to enter something");
</code>
Then catch a NPE.
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|