Last active
August 29, 2015 14:20
-
-
Save jjsub/9c26d9cbd9dff801e557 to your computer and use it in GitHub Desktop.
This file contains 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
<!-- 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