Skip to content

Instantly share code, notes, and snippets.

@jjsub
Last active August 29, 2015 14:20
Show Gist options
  • Save jjsub/9c26d9cbd9dff801e557 to your computer and use it in GitHub Desktop.
Save jjsub/9c26d9cbd9dff801e557 to your computer and use it in GitHub Desktop.
<!-- Good JS simple try catch throw finally 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 {
x = Number(x);
if(x == "") throw "is empty";
if(isNaN(x)) throw "is not a number";
if(x > 10) throw "is too high";
if(x < 5) throw "is too low";
}
catch(err) {
message.innerHTML = "Input " + err;
}
finally {
document.getElementById("demo").value = "";
}
}
</script>
</body>
</html>
try it out on: http://www.w3schools.com/js/tryit.asp?filename=tryjs_finally_error
Read aboit it: http://www.w3schools.com/js/js_errors.asp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment