The controversy is not so much with exceptions, but with Checked Exceptions (e.g. That way you don't have to think of an error indicator yourself. If there is an error you get an exception. Furthermore, the status indicator is standardized. The exception mechanism is valuable, as it is the only way to get a status indicator in combination with a response value. In terms of validation of configuration files (the other use case presented in some of the comments), it's one thing to specify a default if a particular value isn't specified at all - but if the data is formatted wrong (someone types an 'oh' instead of a 'zero' - or they copied from MS Word and all the back-ticks got a funky unicode character), then some sort of system feedback is needed (even if it's just failing the app by throwing a runtime exception). If validation fails, then the user is presented with feedback informing them of what they've done wrong and given a chance to fix it.īinding libraries like JGoodies Binding and JSR 295 make this sort of thing a lot easier to implement than it might sound - and many web frameworks provide constructs that separate user input from the actual business model, only populating business objects after validation is complete. In order for the data to migrate from the user interface model to the business model, it must pass through a validation step (this can occur on a field by field basis, but most scenarios call for validation on the entire object that is being configured). Note that this approach requires you to consider your model in two pieces: the business model (Where we actually care about having values in int or float format) and the user interface model (where we really want to allow the user to put in whatever they want). Once you know that you have properly validated the user input, then it is safe to parse it and ignore, log or convert to RuntimeException the NumberFormatException. In most cases, this is a good design move, even though it can result in multiple calls to parsing algorithms. I'm going to restate the point that stinkyminky was making towards the bottom of the post:Ī generally well accepted approach validating user input (or input from config files, etc.) is to use validation prior to actually processing the data. For all you know, val = Integer.MIN_VALUE is exactly the right option for the application that this completely context free code snippet was take from. The question isn't asking what to return if the number is un-parseable. The question isn't asking about transition between layers. You ignore the question being asked, and answer another question that has nothing to do with it. Some of the answers demonstrate exactly what is wrong with SO. However, I think bad user input is EXPECTED, not rare. There's been a lot of talk on SO about exception handling, and the general attitude is that exceptions should be used for unexpected scenarios only. What's the best practice for handling this situation? I could use a regex to check if it's parseable, but that seems like a lot of overhead as well. The usual way: String userdata = /*value from gui*/ How do you handle input validation without throwing an exceptions? Seeing as Java doesn't have nullable types, nor does it have a TryParse(),
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |