3

My method has a return type that throws NullPointerException.

public class Student {
    public String studentOne() {    
        //code to get name
        if(name == null)
            throw new NullPointerException("Error");
        else
            return name; 
    }
}

my question is .. Should I use public String studentOne throws NullPointerExceptionwhile throwing new exception?

P.S : I know its not bestpractice to throw nullPointerException. But its required in my project

Vikram
  • 2,042
  • 3
  • 14
  • 15
  • 3
    P.S.: What makes you say it's not best practice to throw null pointer exceptions? If you have a null and it shouldn't be allowed, then NPE is just fine. See e.g. http://thecodelesscode.com/case/115 – Louis Wasserman Oct 24 '13 at 18:20
  • @LouisWasserman It might've been something about not explicitly doing `throw new NullPointerException...`, but I agree that there's nothing wrong in and of itself. – Dennis Meng Oct 24 '13 at 18:22
  • This should really be an `IllegalStateException`, IMO. – Dave Newton Oct 24 '13 at 18:32
  • @LouisWasserman I made that comment because of this post.. http://stackoverflow.com/questions/4715492/best-practise-in-catching-and-throwing-nullpointerexception?lq=1 – Vikram Oct 24 '13 at 19:33
  • @asvikki: There are varying opinions as to whether NPE is what you _should_ throw if you get null when it's not allowed. (I lean towards "yes," as the practice followed by both the JDK itself and by Guava.) – Louis Wasserman Oct 24 '13 at 20:34

2 Answers2

6

NullPointerException is an unchecked exception, so you don't need to and should not declare it as thrown. You don't need to do this for any unchecked exception. Of course, if you declare them, they would anyways be ignored by the compiler.

As for throwing a NPE, it's just fine to throw it, when you can't proceed in the method in case the value is null. But it wouldn't make any difference whether you throw it explicitly, or it is implicitly thrown when it is raised. Just that you can pass customized message when you explicitly throw it.

Of course, you can document this behaviour in the method, that it will throw a NPE, under certain circumstances, so as to make the users aware about that.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 3
    That being said, it definitely doesn't hurt to document in the javadoc which cases cause a NPE to be thrown. – Dennis Meng Oct 24 '13 at 18:18
  • what difference does it make in 'throw new NullPointerException' and throws. Does the control gets transferred to calling program in both cases? – Vikram Oct 24 '13 at 18:18
  • 1
    It doesn't make any difference whether or not you declare the `throws`. – Louis Wasserman Oct 24 '13 at 18:20
  • 1
    @asvikki `throw` is used to actually throw an exception. `throws` is just used to declare that a certain exception might be thrown from the method. – Rohit Jain Oct 24 '13 at 18:20
  • I think he is trying to ask if there is difference between using `throw new NullPointerException` and letting the code throw it by itself, i.e. by reaching a point where the null reference is used. The answer is, there is no difference. – Piovezan Oct 24 '13 at 18:21
  • So, you mean to say that, when you throw an exception, it will immediately get thrown with control, remaining in the same program.. rather than searching for exceptional handler over and over again – Vikram Oct 24 '13 at 18:24
  • @asvikki Yes. It would immediately be thrown, and the rest of the method won't get executed. The control will be transferred to the caller. – Rohit Jain Oct 24 '13 at 18:25
  • I should add that for most exceptions you would **have** to use `throws` if you are throwing one somewhere in the method. `NullPointerException` is an example of a `RuntimeException` (or as Rohit said, an **unchecked exception**, none of which need to be declared in a `throws` clause). – dcsohl Oct 24 '13 at 18:40
1

Better solution is to add documentation to method

/**
 * @param  name - not null. NullPointerExcpetion will thrown in case of null
 *
 * @return some string
 */  
public String studentOne(String name)
{
   // ...
}      

There is no difference between

 public String studentOne(String name)  

and

 public String studentOne(String name) throw NullPointerException

because NullPointerException is unchecked exception

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • since you are actually not going to use throws, is it fine to use javadoc tag @throws ?? or should you include that in return javadoc tag? – Vikram Oct 24 '13 at 18:28
  • @asvikki yep, `@throws` it's not a better place. I think in `@param` looks good. Or `@exception` tag too – Ilya Oct 24 '13 at 18:31