• Post Reply Bookmark Topic Watch Topic
  • New Topic

What way for my method if user put wrong parameter?  RSS feed

 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fox example, I have method:



This method return ArrayList with size max.

I have one restriction: my method cant return List with size more 5000 elements (it is just for example);

My question next:

What flow for my method if user run:



I must return Exception? Which one?
I must return null?
I must return List with size 5000?

What ways are the best?

Thanks.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Franko wrote:I must return Exception? Which one?

This is the easiest, and the usual one is IllegalArgumentException.

I must return null?

Definitely not. Return new ArrayList<Integer>(0) before a null.

I must return List with size 5000?

Reasonable; perhaps along with a message that tells the user they're a duffus.

What ways are the best?

There is no "best". It depends entirely on what your requirements are.

Winston
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thank you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Franko wrote:Great! Thank you!

You're most welcome.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!