• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Exceptions to validate inputs  RSS feed

 
Ranch Hand
Posts: 87
Firefox Browser IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to check whether the value passed by an user is valid constant or not. Here is the code I have written.



Now, in above code if the String passed is not withing the listed enum then it throws IllegalArgumentException which is obvious.

But my question is : Is this the proper way to validate? As we are using Java's exception mechanism to validate.

Can someone suggest a better idea or what I have coded above itself is the best option ?

Thanks.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess its fine. But instead of printing the stack trace it would be good if you display a message, something like "the given input <some input> is not a valid one. The valid values are Streaming, Progessive"
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of throwing the Exception is to inform the calling method that there has been an error. Why not put that method invocation into a loop, so you can request new input if the Exception is caught.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!