HTML chapters

Chapter 13 of 17

Error Handling

Try this in our HTML Compiler →

Things go wrong

Files are missing, networks drop, users type nonsense, divisions land on zero. A robust program anticipates errors and handles them, instead of crashing in the user's face. HTML provides a try/catch (or equivalent) mechanism that lets a block of code "throw" an error which a surrounding block can catch and recover from.

<!-- Invalid HTML rarely throws an error. Browsers do their best to render anyway, which is why you should validate your pages: -->
<!-- https://validator.w3.org -->

When to catch

Only catch errors you actually know how to recover from. Catching and then ignoring an error is the single most common cause of mysterious bugs in production systems — the program keeps going as if everything were fine while the data is silently broken.

Logging

Whenever you catch an error, log it with enough context (what you were trying to do, with what inputs) that someone reading the log later can understand what happened. Modern apps live or die by the quality of their logs.

Try it yourself

HTML Compiler
CSS (optional)
Output
Code runs in a sandboxed preview inside your browser — your code is saved locally for next time.