Skip to content

Instantly share code, notes, and snippets.

@Sysetup
Created October 12, 2016 23:10
Show Gist options
  • Save Sysetup/6464e4be6665b5f2ed093a275b094f78 to your computer and use it in GitHub Desktop.
Save Sysetup/6464e4be6665b5f2ed093a275b094f78 to your computer and use it in GitHub Desktop.
try...catch statement. Example.
<!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