In the functions below I verify that the throw works the same as the alert. Can I replace it with an alert? Is there any inconvenience?
var letters = new Array("A","B","C","D","E")
function getLetter(fld) {
try {
var inp = parseInt(fld.value, 10)
if (isNaN(inp)) {
throw "Entry was not a number."
}
if (inp < 0 || inp > 4) {
throw "Enter only 0 through 4."
}
fld.form.output.value = letters[inp]
}
catch (e) {
alert(e)
fld.form.output.value = ""
fld.focus()
fld.select()
}
}
<FORM>
Enter a number from 0 to 4:
<INPUT TYPE="text" NAME="input" SIZE=5>
<INPUT TYPE="button" VALUE="Get Letter" onClick=getLetter(this.form.input)>
Matching Letter is:<INPUT TYPE="text" NAME="output" SIZE=5>
</FORM>
To better understand my doubt I make the same code with alert
and it works the same way: to verify type a non-numeric in the input in both codes
var letters = new Array("A","B","C","D","E")
function getLetter(fld) {
try {
var inp = parseInt(fld.value, 10)
if (isNaN(inp)) {
alert("Entry was not a number.")
}
if (inp < 0 || inp > 4) {
throw "Enter only 0 through 4."
}
fld.form.output.value = letters[inp]
}
catch (e) {
alert(e)
fld.form.output.value = ""
fld.focus()
fld.select()
}
}
<FORM>
Enter a number from 0 to 4:
<INPUT TYPE="text" NAME="input" SIZE=5>
<INPUT TYPE="button" VALUE="Get Letter" onClick=getLetter(this.form.input)>
Matching Letter is:<INPUT TYPE="text" NAME="output" SIZE=5>
</FORM>