Exception Handling in JavaScript

Code for handling Reference Error
 <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <script>
       const k = ["satish","ram"];
       try
       {
          document.write(m[2]);
       }
       catch(e)
       {
  
          document.write("The error is ",e.name);
       }
       finally
       {
         document.write("Hello there I will print");
       }
  
    </script>
  </body>
  </html>
Output
image displaying an reference error in javascript
Code for handling Type Error in JavaScript
 <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <script>
        
       try
       {
          let num=234;
          document.write(num.lastIndexOf("1"));
       }
       catch(e)
       {
          document.write("The error is ",e.name);
       }
  
    </script>
  </body>
  </html 
Output
image displaying type error in javascript
Code for handling Range Error in JavaScript
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <script>
        
       try
       {
          let num=234.8129;
         document.write(num.toFixed(28888888));
       }
       catch(e)
       {
          document.write("The error is ",e.name);
       }
  
    </script>
  </body>
  </html
  
Output
image displaying range error in javascript
Code for handling Syntax Error in JavaScript
 <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body id="d">
    <script>
       
       try
       {
         eval("document.getElementById('d)");
       }
       catch(e)
       {
          document.write("The error is ",e.name);
       }
  
    </script>
  </body>
  </html> 
Output
image displaying syntax error in javascript
Code for demonstrating the use of throw (throwing number) in JavaScript
 <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Defined Exceptions</title>
  </head>
  <body id="d">
    <script>
       let num=3;
       try
       {
         if(num===3)
         {
           throw "this is a new error";
         }
       }
       catch(e)
       {
          document.write(e);
       }
    </script>
  </body>
  </html> 
Output
image displaying an error javascript
Code for Throwing an Object as an Exception in JavaScript
<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <body id="d">
    <script>
       let num=3;
       try
       {
         if(num>2)
         {
           throw new RangeError("Number not allowed");
         }
       }
       catch(e)
       {
          document.write("The error is ",e.message);
       }
  
    </script>
  </body>
  </html>
Output
image displaying an error javascript
Code for demonstrating the use of Finally block in JavaScript
<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Defined Exceptions</title>
  </head>
  <body id="d">
    <script>
       let num=4;
       try
       {
         if(num===3)
         {
           throw new SyntaxError("This is a syntax error");
         }
         else
         {
          document.write("Things are going on fine");
         }
       }
       catch(e)
       {
          document.write(e.message);
       }
       finally
       {
         document.write("finally is executed");
       }
    </script>
  </body>
  </html> 
Output
image displaying an error javascript