JavaScript Page 6 - JavaScript Exception Handling |
JavaScript also allows you to add a "finally" block to the "try-catch" block discussed previously. The "finally" block contains statements that are executed regardless of whether an exception is generated or not in the "try-catch" block.
If an exception is encountered when running the code within the "try" block, JavaScript will stop execution at that point, and look for a "catch" block to handle it. Once the error has been handled (or if no "catch" block exists), control passes to the "finally" block. Take a look at the next example to see how this works: <script language="JavaScript"> Here's the output: The following error occurred: TypeError - 'a' is undefined Thank you for playing. Come back soon!It's important to note that the code in the "finally" block will be executed even if errors are encountered in the corresponding "try" block.
blog comments powered by Disqus |