0

Hi there I am learning OOP by writing a dummy Library management project in Java.

In serachBook(), if book is found in ArrayList, it returns book object, if not found it throws custom exception BookNotFound.

Question 1: Should it just return null and check the returned value at calling code for null or throw custom exception BookNotFound.

Currently I believe BookNotFound is appropriate and am currently doing it. However I am finding one more difficulty: In addbook(), I first call searchBook() to see book already exist, if not it adds the book. However if book doesnt exist searchBook() throws BookNotFound.

Qestion 2: How should I handle this exception thrown by searchBook() in addBook(), since in order to addBook() to insert book in a list, BookNotFound exception must occur. So should I write empty Catch(BookNotFound e){} ?

What could be better OO practice?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

5 Answers5

3

You should return null if it doesn't find book and state it in method document,

Exception throwing is costly operation also it is well suited in exceptional case (like book name you passed null in searchBook() method

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
2

Return null.

Using exceptions to control program flow is a very bad idea. Exceptions are for the exceptional, and a book not found is not an exceptional case.

Throwing an exception is done when your method can't handle a situation and wants to signal that to the caller. You can handle it by returning null.

There are plenty of examples of JDK classes returning null in a similar situation, eg Map.get(key) returns null when the key is not found in the map.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

My suggestion would be to go far an option of returning "null" if the book is not found. Use exceptions only to handle "not so routine" scenarios and from your description "search book" and "add book" looks like a fairly common scenario.

Using exceptions involves overhead:
How slow are Java exceptions?
http://apmblog.compuware.com/2011/04/12/the-cost-of-an-exception/

Community
  • 1
  • 1
coder_bro
  • 10,503
  • 13
  • 56
  • 88
0

Question 1:
Exceptions should be for exceptional conditions.
Book not present dont seems to be a exceptional condition so returning special value like null instead will be more suitable.
http://codeutopia.net/blog/2010/03/11/should-a-failed-function-return-a-value-or-throw-an-exception/

Question 2:
Also never ever eat up the exception.
Instead you can write code to add book in catch block and do some useful stuff like logging.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

You should instead return a null if Book is not found and handle it accordingly.

Throwing Exceptions are expensive operations and as the name would suggest, these are exceptional conditions. So using them in the context of controlling the flow of your application is indeed considered a bad practice.

Moreover, Using exceptions for flow control violates a principle of least astonishment, which make programs hard to read and understand.

Also, compiler vendors do not expect this. They expect exceptions to be thrown rarely, and they usually let the throw code be quite inefficient.

Rahul
  • 15,979
  • 4
  • 42
  • 63