I am interested in Computer science and Mathematics.
Javascript Tutorial 22 Throw and Try to Catch
June
29th,
2017
The try statement lets you test a block of code for errors.
The catch statement lets you handle the error.
The throw statement lets you create custom errors.
The finally statement lets you execute code, after try and catch, regardless of the result.
The throw statement allows you to create a custom error.
Technically you can throw an exception (throw an error).
The exception can be a JavaScript String, a Number, a Boolean or an Object:
throw"Too big";// throw a textthrow500;// throw a number
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;}finally{document.getElementById("demo").value="";}
Modern browsers will often use a combination of JavaScript and built-in HTML validation,
using predefined validation rules defined in HTML attributes: