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?
You should return
nullif it doesn’t find book and state it in method document,Exceptionthrowing is costly operation also it is well suited in exceptional case (like book name you passed null insearchBook()method