• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Exceptions to validate inputs

 
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79467
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic