0

So while editing my question (multiple attempts) I finally found the correct search terms for my original question (see below - I was missing the try-catch part).

Answers I was searching for: Is it expensive to use try-catch blocks even if an exception is never thrown? and/or Which part of throwing an Exception is expensive?

Edited original question: Thanks for the responses everyone, but I can see from the comments that I wasn't clear enough in my question. Sorry about that. Let me hopefully clarify:

I'm new to Java and want to make sure I don't start off forming a bad habit. In Python, I like to validate all user input as early as I can. I often use while loops to do this; however, my projects have been small.

Try-Catch inside While Loop, or many other websites, have examples of while loops that validate a user's input in Java. However, these are also very small code examples.

My question is: Does validating the user's input this way take a lot of processing time/resources? Not just one; but if ALL user inputs were validated this way in a large scale Java project?

The reason I ask is because I worked somewhere in the past (that used Java) where all of the user input wasn't validated. Since I was in quality assurance and support I saw a lot of problems result from this. I'm trying to think of why you wouldn't always use a while loop to validate user input.

Marking as duplicate: Instead of deleting this, I looked at several posts about closing your own question on Meta Stack Exchange. Many of the solutions presented didn't seem to be available for me (probably b/c of lack of privileges). But, I followed the info at: https://meta.stackexchange.com/questions/79916/is-it-possible-to-mark-my-own-question-as-duplicate-of-another - comment - "Close" doesn't appear for me 2011-08-25, but "Flag" did and has a flow to provide an exact duplicate link. – Vincent Scheib Aug 25 '11 at 16:36 - and this worked for me as well.

4
  • 1
    Why would you NOT validate your inputs? What kind of behaviour do you expect on invalid input? Commented Sep 19, 2017 at 22:36
  • Wouldnt you want to validate each control as data is entered rather than iterate through each at the end? from a design perspective, think of what happens when you complete a 'sign up' form. Passwords that dont match are flagged immediately, email addresses that dont match case/pattern are flagged. Same principle, you dont usually validate all at once unless in certain situations i.e. where you must check data server side such as passwords etc. Commented Sep 19, 2017 at 22:37
  • What does 'in a while loop' have to do with it? Commented Sep 20, 2017 at 0:13
  • Every command in every program takes processing time. The amount of processing time depends on how much code you are executing. Commented Sep 20, 2017 at 1:12

0