How to apply try-catch block on exception area in JS?

0 votes
34 views
Ravi Vishwakarma asked 8 days ago in Web Application by Ravi Vishwakarma

How to apply try-catch block on exception area in JS?

1 Answer

0 votes
Ethan Karla answered 6 days ago by Ethan Karla
An exception signifies the presence of an abnormal condition which requires special operable techniques. In programming terms, an exception is the anomalous code that breaks the normal flow of the code. Such exceptions require specialized programming constructs for its execution. Runtime errors, also called exceptions, occur during execution (after compilation/interpretation). For example, the following line causes a runtime error because here the syntax is correct, but at runtime, it is trying to call a method that does not exist.
<script type = 'text/javascript'>

   <!--
      window.printme();
   //-->
</script>
The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions. You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.
  1. The try statement lets you test a block of code for errors.
  2. The catch statement lets you handle the error.
  3. The throw statement lets you create custom errors.
  4. The finally statement lets you execute code, after try and catch, regardless of the result.
Syntax.
<script type = 'text/javascript'>

   <!--
      try {
         // Code to run
         [break;]
      }
      catch ( e ) {
         // Code to run if an exception occurs
         [break;]
      }
      [ finally {
         // Code that is always executed regardless of
         // an exception occurring
      }]
   //-->
</script>
The try block must be followed by either exactly one catch block or one finally block (or one of both). When an exception occurs in the try block, the exception is placed in e and the catch block is executed. The optional finally block executes unconditionally after try/catch.
<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
       <p>Click the following to see the result:</p>
       <form>
           <input type='number' id='val1' placeholder='Enter first value '/>
           <input type='number' id='val2' placeholder='Enter second value '/>
           <input type='button' value='Check Me' onclick='myFunc();' />
       </form>
    <p id='res'></p>
    <script type='text/javascript'>
        function myFunc() {
            var a = document.getElementById('val1').value;
            var b = document.getElementById('val2').value;
            try {
                if (b == 0) {
                    throw ('Doesn't divide by zero');
                } else {
                    var c = a / b;
                    document.getElementById('res').innerHTML='Result is : '+c;
                }
            }
            catch (e) {
                alert('Error: ' + e);
                document.getElementById('res').innerHTML = e;
            }
        }
    </script>
   </body>
</html>