JavaScript Error Handling Part – 3

Free Web development courses with real-time projects Start Now!!

Program 1

<html>
    <head>
        <script>
             function devide()
             {
                try
                {
                     let str=prompt("Enter a String")
                      alert(str)
                    let i=parseInt(prompt("Enter an index number for dispaly character"))
                    
                     let n=str.length;
                     if(i>=0 && i<n)
                     alert(str.charAt(i))
                     else
                     throw new Error("Index out of bound error")
                //    let b=parseInt(prompt("Enter Second Number"))
                //    let c
                //    if(b==0)
                //          throw new Error("Unable to devide by zero")
                //    c=a/b
                //    alert("Division is "+c)
                }
                catch(e)
                {
                      alert(e.message)
                }

             }
        </script>
    </head>
 
    <body>
      <center>
          <form>
            <input type="button" value="click here" onclick="devide()">
            </form>
     </center>
    </body>
</html>

 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

Leave a Reply

Your email address will not be published. Required fields are marked *