Learn JavaScript

Lesson 4 of 9 ¡ Error Handling: throw, try, catch, finally, and Reading a Stack Trace

Module 10 ¡ Error Handling: throw, try, catch, finally, and Reading a Stack Trace

When to Throw and When to Return: Exceptions, null, Result Objects, and What to Let Crash

Free with an accountReading

Tanvir and Lima argue about one function. It turns a time like "07:45" into minutes since midnight. Tanvir wants it to throw on bad text, "so nobody can ignore it". Lima wants it to return null, "because throwing for a typo is dramatic". Both have seen code where the other choice went wrong.

Checking what this lesson needs for youâ€Ļ

What is inside When to Throw and When to Return: Exceptions, null, Result Objects, and What to Let Crash

  1. Three ways to say "it did not work"
  2. Return null when absence is a normal answer
  3. Return a result object when the caller must decide every time
  4. Throw when the caller cannot sensibly go on
  5. The honest rule
  6. The top level: log, count, and move on
  7. What to let crash