user471011
user471011

Reputation: 7374

java. Method return Null. What exception I must generated?

I have this line code:

String name = Book.getName();

/*next lines of code*/

Next, variable name processing in other code without any checks. In some cases, possible situation, when name=null and other code will exit with an error.

It is bad.

Also, I cant access to the other code.

So, what do you think, my next implementation is correct:

    try
    {
    String name = Book.getName();
    if(null== name)

    throw new NullPointerException("method 'getName' return null");

/*next lines of code*/
    }

    catch(NullPointerException e)
    {
    System.out.print("Hey! Where book name? I exit!");
    System.exit();
    }

I have any other choose in this case?

It is possible to generate any other type of Exception or only NullPointerException?

Thanks.

Edit:

Ok,

  String name = Book.getName();

it's imagine code line. In real case, I have more complex code:

List<Book> bookList= new ArrayList<Book>();
String name = null;

Iterator i = BookShop.getBooks.iterator(); //BookShop it is input parameter!

while(i.hasNext())
{

Book book = (Book) i.next;
name = book.getName();

nameList.add(name);


}

This example more full.

So, in this code input parameter BookShop Object.

What problem I can have with this Object?

  1. BookShop can be NULL;
  2. method BookShop.getBooks() can return NULL;

Also, getName() can return NULL too.

So, general problem next: there is no guarantee the correctness of input parameter BookShop!

And I must to consider every possible option (3 NULL)

For me, add General try-catch block and that all.

No?

Upvotes: 1

Views: 1548

Answers (7)

Mark Jeronimus
Mark Jeronimus

Reputation: 9543

It's more friendly for Java not to use exceptions, but just check the return value

String name = Book.getName();
if (name == null)
    System.out.print("Hey! Where book name? I exit!");
else {
    /*next lines of code*/
}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

Exceptions should not be used for normal control flow. Just use the if block:

String name = Book.getName();
if (name == null) {
    System.out.print("Hey! Where book name? I exit!");
    System.exit();
}

/*next lines of code*/

Upvotes: 1

Matthias
Matthias

Reputation: 12259

You don't need to throw an Exception in this case - just handle the null value and you are fine.

Upvotes: 0

Gio
Gio

Reputation: 1954

Using try and catch in this case is unneeded. You can just write like this:

if(Book.getName() != null)
   String name = Book.getName();
else
   //handle the situation with null

Upvotes: 0

Jivings
Jivings

Reputation: 23250

Your code is a bit iffy, but I assume you're learning. You don't need to throw the NullPointerException explicitly, you can throw whatever Exceptions you like. But you probably don't really need the Exception catching here, you can just check for the null and handle the situation appropriately if it's true.

Also, please avoid Yoda conditions. Your if statement should read

if name is null

so

if (name == null)

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500375

I would probably use IllegalStateException:

 String name = Book.getName();
 if (name == null) {
     throw new IllegalStateException
         ("Method foo must not be called when the book has no name");
 }

It really depends on where the state is coming from though - it's not really clear what's going wrong here.

I certainly wouldn't start catching NullPointerException - exceptions like that (and the illegal state one) shouldn't be explicitly caught. Let them bubble up, and if it's appropriate have some sort of top-level handler.

Upvotes: 1

talnicolas
talnicolas

Reputation: 14053

You can create any exception you like by extending the Exception class, like a NoNameProvidedException for example. There are a lot of example one Google to help you do that.

I guess in your case just checking with an if if the name is null should be sufficient as you just want to do a System.exit().

Upvotes: 3

Related Questions