JavaScript Errors

There a are different type of error in alll the programming languages. In JavaScript there are Syntax error, Runtime errors, Logical errors etc.
To Handle these errors JavaScript try catch block is used.
While try statment is used test the block of code.
The catch statement is used to handle the error.
The throw statement is used to create custorm error.
The finaly statement is used to throw a result after all the process.

Syntax of JavaScript try, catch and finaly

try {
  Block of code to try
catch(err) {
  Block of code to handle errors
finally {
  Block of code to be executed regardless of the try / catch result


A regular expression could be defined with the RegExp () constructor, as follows −

<!DOCTYPE html>

<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="p01"></p>

function myFunction() {
  const message = document.getElementById("p01");
  message.innerHTML = "";
  let x = document.getElementById("demo").value;
  try { 
    if(x == "")  throw "is empty";
    if(isNaN(x)) throw "is not a number";
    x = Number(x);
    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 = "";


Output of above code

Please input a number between 5 and 10:

The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.