try-catch.html 604 B

123456789101112131415161718192021
  1. <html>
  2. <head>
  3. <title>Testing getElementById with a try catch statement</title>
  4. </head>
  5. <body>
  6. <div id="foo">Some text</div>
  7. <script>
  8. try {
  9. document.getElementById(undefined);
  10. document.getElementsByClassName(undefined);
  11. document.getElementsByTagName(undefined);
  12. } catch(err) {
  13. console.log('Error found: ' + err);
  14. throw new Error("I detect an error!");
  15. }
  16. document.getElementById('foo');
  17. </script>
  18. </body>
  19. </html>