try-catch.html 655 B

12345678910111213141516171819202122
  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. document.querySelector(undefined);
  13. } catch(err) {
  14. console.log('Error found: ' + err);
  15. throw new Error("I detect an error!");
  16. }
  17. document.getElementById('foo');
  18. </script>
  19. </body>
  20. </html>