Computer >> Computer tutorials >  >> Programming >> Javascript

what is the significance of finally in javascript?


Finally

The finally statement lets you execute code, after try and catch, regardless of the result.The following example would give result from finally block even though catch block got the error. 

Example

<html>
<body>
<pre>
<script>
   function addition() {
      document.writeln("every one should be good at addition");
   }
   function substraction(){
      document.writeln("substraction helps you");
   }
   try {
      addition()
      division()
   }
   catch(err) {
      document.writeln("be good at division : " + err);
   }
   finally {
      substraction()
   }
</script>
</pre>
</body>
</html>

Output

every one should be good at addition
be good at division : ReferenceError: division is not defined
substraction helps you