Created
October 12, 2016 23:10
-
-
Save Sysetup/6464e4be6665b5f2ed093a275b094f78 to your computer and use it in GitHub Desktop.
try...catch statement. Example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<p>Please input a number between 5 and 10:</p> | |
<input id="demo" type="text"> | |
<button type="button" onclick="myFunction()">Test Input</button> | |
<p id="message"></p> | |
<script> | |
function myFunction() { | |
var message, x; | |
message = document.getElementById("message"); | |
message.innerHTML = ""; | |
x = document.getElementById("demo").value; | |
try { | |
if(x == "") throw "empty"; | |
if(isNaN(x)) throw "not a number"; | |
x = Number(x); | |
if(x < 5) throw "too low"; | |
if(x > 10) throw "too high"; | |
} | |
catch(err) { | |
message.innerHTML = "Input is " + err.message; | |
} | |
finally { | |
return false; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment