Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is better, using exceptions or if statements

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I am rather new to java (about a year's worth of programming) and I am wondering what is the best method of error checking inputs on a form. In VB I would simple retrieve the contents of a text box and use if statements to test it for valid entries. To me this would seem the easiest solution, but I want to use the more OOP way which I believe is using exceptions.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JTextField r = new JTextField(10);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double d = Double.parseDouble(r.getText().Trim())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>The above code simpet is not complete. It is obvious that some sort of test is required to make sure that the data entered into r is numeric. How would one use exceptions in this case to write text back to the box if the data is none numeric?<br><br>Any Help would be appreciated<br><br>fenris<br><br>
 
What I've heard is that exceptions should be reserved for truly exceptional situations (to use a pun).&nbsp;&nbsp;I would normally <i>expect</i> any users to screw up an input field, so I wouldn't consider it exceptional.&nbsp;&nbsp;I vote for using if statements to check for stupid things, surrounded by a try..catch to pick up anything that falls through.<br><br>Chip H.<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top