• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help exception handling  RSS feed

 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to implement an orderlist (actually an arraylist) and to ensure that it allows additon of unique orders only else it rejects the oepartion by throwing an exception.

Here is my code------------



i am getting the OrderNotFoundException but can't get the OrderAlreadyExistException. Please help !
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change your code:


into:


because method add in ArrayList doesn't return false if there are two object equals.

cheers,
Ricky
 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ricky. Now i can get the OrderAlreadyExistException. But can you clarify when or under which conditions the add method of ArrayList returns false ?
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at the API Specification in Class Collection.

it returns:
true if this collection changed as a result of the call

so, i think it return false if the collection didn't change as a result of call.

just as simple as that

cheers,
Ricky
 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ricky for the simple answer. Does the boolean return type of add() method of collection interface specifies the result of success or failure of add() method ? If its so, then the method should return false for unmodifiable collection retrieved through Collections.unmodifiableList(List list) .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!